#557 √ invalid
Ben Mabey

Multiple expectations set on same object along with stub! give false-negatives

Reported by Ben Mabey | October 3rd, 2008 @ 06:49 PM | in No-Milestone-Assigned

Prior to the most recent releases (1.1.5-1.1.8) I had the follow spec which passed fine:



describe "#write_xml" do

  before(:each) do
    @io = StringIO.new
    File.stub!(:open).and_yield(@io)  # Note, how I am stubbing this here.
  end


 it "should write one file per page" do
     # Given
     exporter = ...
     ...

     # Expect
     # here I need to do multiple expectations with different args:
     File.should_receive(:open).with(/_page_1.xml/, "w").once.and_yield(mock("file", :<< => true))
     File.should_receive(:open).with(/_page_2.xml/, "w").once.and_yield(mock("file", :<< => true))

     # When
     exporter.write_xml
   end

end

With the new rspec release the above spec fails on the second expectation set on File.open. It says that /page_2.xml/ is never given as an argument to FIle.open. I know that it is working and that this a false-negative that was introduced in this version.

If I remove my stub! call in the before block the spec passes just fine. So I believe this break is due to the new changes Pat added to the stub and mock methods. I believe that the stubbed return value is being returned the second time instead of the second expectation being validated.

I maybe setting multiple expectations incorrectly... If that is the case please let me know what the correct usage is.

Thanks, Ben

Comments and changes to this ticket

  • Pat Maddox

    Pat Maddox October 3rd, 2008 @ 08:12 PM

    I think you're right, it's just a bug. I'll take a look at it.

  • Pat Maddox

    Pat Maddox October 3rd, 2008 @ 09:40 PM

    Hey Ben,

    I've looked at this a bunch and so far I've been unable to replicate it. Here's the basic example I've written:

    
    describe "An RSpec Mock" do
      it "should handle multiple constrained expectations following a similar stub" do
        File.stub!(:open).and_yield :blah
        File.should_receive(:open).with(/first/).and_yield(:first)
        File.should_receive(:open).with(/second/).and_yield(:second)
        File.open("this is first call") {|o| o.should == :first }
        File.open("this is second call") {|o| o.should == :second }
        File.rspec_verify
      end
    end
    

    I've tried a bunch of different things with this...started off with a basic mock object, then did it on a class in case it was that, then tried it on File.open. I've put in "should_receive(:open).once", used multiple args in the with(). So far I simply have not been able to make it fail. Can you give me a bit more detail about your environment that might help me track it down? Also if you could paste more code (the example and implementation) then that would certainly be helpful.

  • Ben Mabey

    Ben Mabey October 3rd, 2008 @ 09:43 PM

    I am using Rails 2.0.2, would that be an issue? I'll try to get a more contained example to you so you don't need my whole app to run it. :)

  • Pat Maddox

    Pat Maddox October 9th, 2008 @ 01:24 AM

    Hey Ben, any news on a failing example?

  • Ben Mabey

    Ben Mabey October 9th, 2008 @ 01:30 AM

    Hey Pat, I worked on it some more today.. I can't seem to reproduce the problem either. I'm trying to figure out why my particular example fails with the new rspec. Until I figure it out lets mark this ticket as invalid. If I find the problem and it is in fact an rspec problem them I'll open the ticket up again. Sorry for any time you spent on this.

    Thanks, Ben

  • Pat Maddox

    Pat Maddox October 9th, 2008 @ 01:37 AM

    • → State changed from “new” to “invalid”

    Okay no problem man. Just reopen when you find something.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Behaviour Driven Development for Ruby.

Shared Ticket Bins

People watching this ticket