[rspec-users] stub_chain together with should_receive

medihack kai.schlamp at googlemail.com
Thu Nov 25 10:44:58 EST 2010


I am trying to test if in a method calling chain one of the methods
get a specific parameter. In the below code for example MyModel must
receive the parameter 0 for the method `offset`. Unfortunately the
code below does not work. It seems it is not possible to mix
should_receive and stub_chain. How could I solve this? I am using
RSpec 2.

does not work:
MyModel.should_receive(:offset).with(0).stub_chain(:tag_counts, :offset, :limit, :order).and_return([])

also does succeed (expectation is never called):
MyModel.stub_chain(:tag_counts, :offset, :limit, :where, :order).and_return([])
MyModel.should_receive(:offset).with(0) { MyModel }

does succeed, but is IMHO quite unhandy (you can't stub_chain in a
before filter):
MyModel.stub(:tag_counts) { MyModel }
MyModel.should_receive(:offset).with(0) { MyModel }
MyModel.stub_chain(:limit, :where, :order).and_return([])

More information about the rspec-users mailing list