1

I'm using rspec and the rspec mocks library for stubbing/mocking, but I'm starting to think that the line between stubbing out a method and asserting how it's been used is blurred. In my tests I'll often write something like:

before(:each) do
  subject.should_receive(:sum).once.with([1, 2, 3]).and_return(6)
end

it("should do something that relies on sum") do
  subject.call_something([1, 2, 3]).should == 24
end

But what are my assertions? That call_something([1, 2, 3]):

  • Returns 24
  • Calls sum([1, 2, 3]) during it's execution

However, there is only one it block - the other assertion is hidden away in the stub definition. To put it another way, my stub is also my assertion. Wouldn't it be much clearer to separate the two, and put an explicit assertion in for how the stubbed method was called:

before(:each) do
  # set up what my stub should return for a given input
  subject.may_receive(:sum).with([1, 2, 3]).and_return(6)
end

# assert how my stub was actually called
it("should have called sum with 1, 2, 3") do
  # this is pseudo-rspec
  subject.call_something([1, 2, 3]).should have_called(:sum).on(subject).once.with([1, 2, 3])
end
it("should do something that relies on sum") do
  subject.call_something([1, 2, 3]).should == 24
end

This way it's very clear what I'm asserting because my stub definitions and assertions have been separated. I can setup my test at the top, and then check it's behaviour at the bottom, without mixing the two.

So, my question is whether there's a way of doing this? Most mocking frameworks work in the same way as rspec-mocks, and define the contract for how a stub method is used along with the expected behaviour, and then automagically check the assertion at the end.

My point is a conceptual one about how BDD works, and might be a bit subtle. Let me know I need to clarify it further!

1 Answer 1

2

You're correct. should_receive is an assertion (more accurately it is an expectation) and should not go in a before block. If you need the stubbing side-effect of should_receive, then stub separately in your before:

before do
  subject.stub(:sum).and_return(6)
end

it "should do something that relies on sum" do
  subject.should_receive(:sum).once.with([1, 2, 3]).and_return(6)
  subject.call_something([1, 2, 3]).should == 24
end

This keeps the stubbing and expectation separate and where they belong.

2
  • That looks good.But what if I need to call sum multiple times with different arguments and get different results each time? (That's why I've traditionally avoided using stub) Mar 13, 2012 at 3:23
  • If the return value of a stub changes then the test should likely be in a different context block, in which case you can use lets to reduce duplication of before blocks. Mar 13, 2012 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.