When you are testing a method like the following contrived example:
def make_pony_eyes_pink
before, Pony.eye_color = Pony.eye_color, :pink
yield
ensure
Pony.eye_color = before
end
Don’t do it with expectations such as these:
it "changes the eye color to pink for the duration of the block" do
Pony.expects(:eye_color=).with(:pink)
pony.make_pony_eyes_pink {}
Pony.eye_color.should.be :black
end
Your expectation would still succeed if the implementation of the method would change to reset the eye color before yielding the block:
def make_pony_eyes_pink
before, Pony.eye_color = Pony.eye_color, :pink
Pony.eye_color = before
yield
end
Therefore, check that what you describe is actually what you get:
it "changes the eye color to pink for the duration of the block" do
eye_color_during_block = nil
pony.make_pony_eyes_pink do
eye_color_during_block = Pony.eye_color
end
eye_color_during_block.should.be :pink
Pony.eye_color.should.be :black
end
Astute readers will note that I do not make the assertion inside the block, because that would mean the assertion could potentially *not* run without making the test fail.