Introduction to Ruby Testing with RSpec

github.com/deniseyu

At a high level,

Testing code allows developers to know that the code is working as expected,

no more, and no less.

Test Driven Development: Why?

  • 'What?' before 'How?'
  • Refactoring is easy
  • Maintainability

Why TDD

Using RSpec

deniseyu@Winter-Is-Coming » gem install rspec

Successfully installed rspec-3.2.0
Parsing documentation for rspec-3.2.0
Installing ri documentation for rspec-3.2.0
Done installing documentation for rspec after 0 seconds
1 gem installed

deniseyu@Winter-Is-Coming » rspec spec/something_awesome_spec.rb

Ex: fizzbuzzing

require 'fizzbuzz'

describe Fizzbuzz do

    it 'says "Fizz" on a number that divides by 3' do
        fizzbuzz = Fizzbuzz.new
        expect(fizzbuzz.says(3)).to eq 'Fizz'
    end

end

spec/fizzbuzz_spec.rb

Ex: fizzbuzzing

Ex: fizzbuzzing

class Fizzbuzz
  def says(number)
    'Fizz' if number % 3 == 0
  end
end

lib/fizzbuzz.rb

Ex: fizzbuzzing

Take that, failing tests!

Questions?

Introduction to Ruby Testing with RSpec

By Denise Yu

Introduction to Ruby Testing with RSpec

  • 1,713