[rspec-users] Cherry-picking mocks?!

Pat Maddox pergesu at gmail.com
Mon Jun 2 14:50:56 EDT 2008

On Mon, Jun 2, 2008 at 11:38 AM, Rick DeNatale <rick.denatale at gmail.com> wrote:
> A colleague just came in and asked me about a problem he was having with
> stub_render, which reminded me of another issue he had a week or so ago,
> which seems related.
> Let me start with the earlier issue.
> He was trying to write specs for a method which sends the same message
> possibly multiple times, but with different arguments under different
> calling conditions. He wanted to write
>    something.should_receive(:foo).with(someArguments)
> except that the other calls would cause this to fail. He was trying to get
> this to work by specifying ordered expectations but this was getting messy.
> What he really wanted was some kind of paired mock and stub, so that the
> expectation that the method would be invoked with the given arguments could
> be verified, but the other calls would just be stubbed.
> Now after today's question, and understanding how expect_render and
> mock_render differ from normal expectations, these seem to be related.
> Maybe we should be able to say something like:
> something.expects(:foo).with(someArguments).with(otherArguments).otherwise(:stub)
>   something_else.expects(:bar).with(arguments).otherwise(:pass)    # if the
> arguments don't match pass the method to the real object
> Which is really just a strawman to see if a discussion ensues.  There are
> other issues like providing return values, and perhaps optionally passing a
> method through after verification that it was called with the expected
> arguments.
> Comments?

I've never used *_render before so I'm going to ignore that part.

I just tried out

describe "Mixed mocks and stubs" do
  before(:each) do
    @object = Object.new
    @object.stub!(:foo).and_return 1

  it "should allow both mocking and stubbing" do
    @object.should_receive(:foo).with(:mock).and_return 2
    @object.foo(:mock).should == 2 #mock
    @object.foo.should == 1 #stub

and it passes.  Would that be helpful to your coworker's situation?


More information about the rspec-users mailing list