Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Stubbing a method on any_instance twice causes that method to be undefined in later tests #54

@bgentry

Description

@bgentry

I encountered a weird issue with 2.6.0.rc2 where I was stubbing a method on any_instance of a model. In the model specs (which were run after the controller specs) my tests were showing that the method was undefined.

from builds_controller_spec.rb:

before do
  Build.any_instance.stub(:already_built?).and_return(false)
end

it "should not create any builds when both builds already exist" do
  Build.any_instance.stub(:already_built?).and_return(true)
  post :create, :build => { :slot => 'something' }
  Build.count.should == 0
end

from build_spec.rb:

  it { should respond_to :already_built? }

If I run the controller spec first, I see this:

 Failure/Error: it { should respond_to :already_built? }
   expected #<Build id: nil ...> to respond to :already_built?

If I run the tests in the opposite order, everything is fine. I've narrowed the cause down to the specific any_instance line in the controller spec because if I comment out that one line, all of the other tests pass.

My assumption is that because I'm stubbing the same method on any_instance twice, it actually deletes the real method when cleaning up the 2nd time after the spec block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions