43

In writing Rspec tests, I'm often frustrated with should_receive. I'd like to know if there's a less intrusive alternative.

For example:

describe "making a cake" do
  it "should use some other methods" do
    @baker.should_receive(:make_batter)
    @baker.make_cake
  end
end

The call to should_receive is a good description, but it breaks my code, because should_receive works by masking the original method, and make_cake can't proceed unless make_batter actually returns some batter. So I change it to this:

@baker.should_receive(:make_batter).and_return(@batter)

This is ugly because:

  • It looks like I'm testing that make_batter correctly returns @batter, but I'm actually forcing the fake version of make_batter to return that.
  • It forces me to separately set up @batter
  • If make_batter has any important side effects (which could be a code smell, I suppose) I have to make those happen, too.

I wish that should_receive(:make_batter) would verify the method call and pass it on to the original method. If I wanted to stub its behavior for better isolation testing, I would do so explicitly: @baker.stub(:make_batter).and_return(@batter).

Is there a way to do something like should_receive without preventing the original method call? Is my problem a symptom of bad design?

3 Answers 3

63

It looks like the nicer API to delegate to the original method that Myron Marston alluded to has actually been added in rspec-mocks v2.12.0

So now you can simply do this any time you "want to set a message expecation without interfering with how the object responds to the message":

@baker.should_receive(:make_batter).and_call_original

Thanks for adding this, Myron.

1
  • 2
    And thank you for revisiting this question with an updated answer. Feb 4, 2013 at 19:48
11

You can have should_receive run the original method like so:

@baker.should_receive(:make_batter, &@baker.method(:make_batter))

Both should_receive and stub support passing a block implementation (that is eval'd when the method is called). &@baker.method(:make_batter) gets a handle to the original method object, and passes it along as the block implementation.

FWIW, we'd like to provide a nicer API to delegate to the original method (see this issue), but it's difficult to add that functionality without breaking backwards compatibility.

4
  • Neat! I didn't realize should_receive could take a block. Also good to see that I'm not the only one who wants this. Aug 28, 2012 at 14:29
  • Alternate (possibly unworkable) idea: what if, in my example, the test simply bailed from bake_cake once make_batter had been received? I shouldn't be testing more than one thing at a time anyway, right? Aug 28, 2012 at 14:33
  • I created a little helper method for this: def should_receive_and_proceed(object, method); object.should_receive(method, &object.method(method)); end
    – trliner
    Oct 26, 2012 at 5:25
  • Given it is 2016 now, I think @Tyler Rick's answer above is more appropriate: stackoverflow.com/a/14693779/224707
    – Nick
    Sep 6, 2016 at 11:12
6

You're having this problem with should_receive because you're testing implementation details of the make_cake method. When writing tests you should only focus on behavior and not on a sequence of internal method calls. Otherwise refactoring your code later would result in a huge refactoring of all your tests as well.

Mocks and Stubs come in handy when you want to test your classes in isolation. When writing unit tests your test subject should be isolated from any other object. Both act as a stand-in when you're working with multiple objects at once. In this case you can use should_receive to ensure that your test subject correctly delegates some task to another object by calling a method.

3
  • 1
    Your overall point about not testing implementation details feels correct, but it seems that's the only use case for should_receive, right? I'm still unclear how should_receive(:foo) can be used at all without and_return if foo is supposed to return something. Aug 28, 2012 at 14:27
  • Also, probably my lousy example obscures the issue. Suppose I've written a wrapper method for some library. I want to test that my wrapper calls the underlying library method with the correct arguments, and my method also needs to parse what it gets back before returning. I think that puts me back to some_library.should_receive(:some_method).with('foo', 'bar').and_return('thingy'), right? In this case, I'm not testing an implementation detail, but the purpose of the wrapper method. Aug 28, 2012 at 14:38
  • In this case you of course want to return something. But in order to keep up the isolation I'd fake the return value from the library. (A good example would be a library that wraps a web service. I wouldn't want my tests to call out for the web service every time I run them and I don't want to rely on all the parts that could go wrong during a call to it) That's of course something I'd only do in unit tests. When writing integration tests I'd want all levels of the implementation the be triggered. Aug 28, 2012 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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