The document contains snippets of Ruby code and discussions around test-driven development (TDD) and behavior-driven development (BDD). It includes code examples using RSpec to test an AgeCalculator class, discussions of gems like rspec and test frameworks like Test::Unit. It also covers observer pattern and use of test doubles in RSpec tests. Live coding examples are presented on FizzBuzz implementation using TDD and RSpec best practices.
1 of 87
Download to read offline
More Related Content
Let's begin Behavior Driven Development using RSpec
18. module System
class AgeCalculatorTest < Test::Unit::TestCase
include RR::Adapters::TestUnit
def setup
@calc = AgeCalculator.with_birthday(1981, 7, 20)
end
def test_age_on
assert_equal(29, @calc.age_on(2011, 7, 19))
assert_equal(30, @calc.age_on(2011, 7, 20))
end
def test_age_today
d, m, y = [*Time.now][3..5]
stub(@calc).age_on
@calc.age_on_today
assert_received(@calc) {|c| c.age_on(y, m, d) }
end
end
end
2011 5 22
20. module System
describe AgeCalculator do
context `with birthday 1981-07-20¨ do
subject { AgeCalculator.with_birthday(1981, 7, 20) }
describe `#age_on¨ do
context `with 2011-07-19¨ do
specify `the age is 29¨ do
subject.age_on(2011, 7, 19).should be == 29
end
end
context `with 2011-07-20¨ do
specify `the age is 30¨ do
subject.age_on(2011, 7, 20).should be == 30
end
end
end
describe `#age_on_today¨ do
it `calls age_on with year, month, and day on today¨ do
d, m, y = [*Time.now][3..5]
subject.should_receive(:age_on).with(y, m, d)
subject.age_on_today
end
end
end
end
end
2011 5 22
34. module System
describe AgeCalculator do
context `with birthday 1981-07-20¨ do
subject { AgeCalculator.with_birthday(1981, 7, 20) }
describe `#age_on¨ do
context `with 2011-07-19¨ do
specify `the age is 29¨ do
subject.age_on(2011, 7, 19).should be == 29
end
end
context `with 2011-07-20¨ do
specify `the age is 30¨ do
subject.age_on(2011, 7, 20).should be == 30
end
end
end
describe `#age_on_today¨ do
it `calls age_on with year, month, and day on today¨ do
d, m, y = [*Time.now][3..5]
subject.should_receive(:age_on).with(y, m, d)
subject.age_on_today
end
end
end
end
end
2011 5 22
62. Photo by rla579: http://?ickr.com/photos/rla579/2482286520/
2011 5 22
63. Working Effectively with Legacy tDiary Code
using Cucumber and RSpec
KAKUTANI Shintaro; Eiwa System Management,Inc.; Nihon Ruby-no-kai
http://kakutani.com/articles/working_effectively_with_legacy_tdiary_code_using_cucumber_and_rspec.pdf
2011 5 22
76. describe FizzBuzzCounter do
describe `.filter¨ do
context `with 1¨ do
it `returns 1¨ do
FizzBuzzCounter.
filter(1).should be == 1
end
end
end
end
2011 5 22
77. describe FizzBuzzCounter do
describe `.filter¨ do
context `with 1¨ do
it `returns 1¨ do
FizzBuzzCounter.
filter(1).should be == 1
end
end
end
end
2011 5 22
85. describe ConcreteObservable do
describe `#change¨ do
it `calls its notify()¨ do
subject.should_receive(:notify)
subject.change
end
end
end
2011 5 22
86. describe ConcreteObservable do
describe `#change¨ do
subject { ConcreteObservable.new }
it ^calls observers¨ update() ̄ do
observer = double(`an observer¨)
observer.should_receive(:update)
subject.change
end
end
end
2011 5 22