Is there a way to undo Mocha stubbing of any_instance?

Posted by Steve Weet on Stack Overflow See other posts from Stack Overflow or by Steve Weet
Published on 2010-04-23T08:31:24Z Indexed on 2010/04/23 8:33 UTC
Read the original article Hit count: 237

Filed under:
|
|
|

Within my controller specs I am stubbing out valid? for some routing tests, (based on Ryan Bates nifty_scaffold) as follows :-

it "create action should render new template when model is invalid" do
  Company.any_instance.stubs(:valid?).returns(false)
  post :create
  response.should render_template(:new)
end

This is fine when I test the controllers in isolation. I also have the following in my model spec

it "is valid with valid attributes" do
  @company.should be_valid
end

Again this works fine when tested in isolation. The problem comes if I run spec for both models and controllers. The model test always fails as the valid? method has been stubbed out. Is there a way for me to remove the stubbing of any_instance when the controller test is torn down.

I have got around the problem by running the tests in reverse alphabetic sequence to ensure the model tests run before the controllers but I really don't like my tests being sequence dependant.

© Stack Overflow or respective owner

Related posts about rspec

Related posts about mocha