Currently I’m grinding through the process of translating my old unit tests on a project into much nicer Rspec tests. Over all it has been a good experience. Especially since I caught some things in the process that the old tests didn’t cover.
So I started on new controller and I wrote something like this:
describe "My Cool Test" do
before(:each) do
@obj = String.new
puts "Hello"
@obj.should_receive(:stupid_method).once
end
it "should do something"
it "should do something else"
it "should not do this"
end
The object never calls stupid_method so it should fail the expectation right?
Well it turns out that although the begin is executed - because all of the examples are pending! If you run the above code you’ll see “Hello” several times - but no error.
Now add in the following example:
it "should be empty" do
@obj.should == ""
end
You’ll get:
Spec::Mocks::MockExpectationError in ' My Cool Test should be empty'
Mock 'String' expected :stupid_method with (any args) once, but received it 0 times
So there you go - (And don’t worry I submitted a bug)
You may be wondering why I said triggers a lot of work - well I’m currently using the Rspec mock library. I was very close to switching to mocha - which would have meant modifying a heck of a lot of tests.