[rspec-users] stubbing a method that yeilds sequential results

Mikel Lindsaar raasdnil at gmail.com
Sun Oct 14 03:28:31 EDT 2007


On 8/13/07, rupert <rupert_apsc at rupespad.com> wrote:
> On 12 Aug 2007, at 14:38, David Chelimsky wrote:
> >> However, what I actually need to do is check each result that is
> >> yielded by the Connector.each_result method and compare it to the
> >> previous one.  If they are sufficiently similar I need to merge them
> >
> > I'm pretty sure you can get what you want by using should_receive
> > instead of stub and doing this:
> >
> > Connector.should_receive(:results).and_yield(@result1)
> > Connector.should_receive(:results).and_yield(@result2)

David, I am having the same mind meld here as Rupert.

I am trying to stub out Net::POP3 which yeilds a block for each
message returns.  The code (snipped down) that I am trying to mock out
is:

Net::POP3.delete_all(@server, @port, @username, @password) do |m|
  # do stuff with mail
end


Doing

Net::POP3.should_receive(:delete_all).and_yield(@message1)

Works fine.

Net::POP3.should_receive(:delete_all).and_yield(@message1)
Net::POP3.should_receive(:delete_all).and_yield(@message2)

Gives:

1)
Spec::Mocks::MockExpectationError in 'GetMail downloading email should
download some mail'
Mock 'Class' expected :delete_all with (any args) once, but received it 0 times
./spec/getmai_spec.rb:86:


It is almost like the second mock overrides the first mock ability to
intercept the call.

Stubbing it does the expected behaviour of overwriting the last stub, ie:

Net::POP3.stub!(:delete_all).and_yield(@message1)
Net::POP3.stub!(:delete_all).and_yield(@message2)

Returns no error, but the block is only called once, not twice.

Any further musings on this?

Regards

Mikel


More information about the rspec-users mailing list