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

Mocking models is not isolated #916

Closed
molfar opened this issue May 23, 2013 · 5 comments
Closed

Mocking models is not isolated #916

molfar opened this issue May 23, 2013 · 5 comments

Comments

@molfar
Copy link

molfar commented May 23, 2013

I mock a model in my controller spes (terms_controller.rb). Also I use model (non exsisting yet) without mocking it in my acceptance test (term_display_spec.rb). And I see a strange situation.

If I run acceptance test separatly as
bundle exec rspec spec/features/term_display_spec.rb - everything goes the right way, I get an error NameError: uninitialized constant Term. It's OK, because I really dont have Term model yet.

But, if I run my acceptence test with controller spec in one command as
bundle exec rspec spec/features/term_display_spec.rb spec/controllers/terms_controller_spec.rb
I suddenly get another error, undefined method `create' for Term:Class, that means that somewhere class Term already exsists! I think that mocking something in one test (inside describe block) should not affect another tests. Explaine me pleaase this situation.

My working code is here https://github.com/molfar/sample_rspec

@alindeman
Copy link
Contributor

This is really an rspec-rails issue, but tl;dr is mock_model defines a constant and this is expected behavior.

Check out the docs:

@molfar
Copy link
Author

molfar commented May 23, 2013

I think mock_model should define a constant only in its outer scope of describe or "it" block, but not globally. How about independent tests?

@alindeman
Copy link
Contributor

I think mock_model should define a constant only in its outer scope of describe or "it" block, but not globally. How about independent tests?

This isn't really possible. Even if it were, the code under test wouldn't be able to use the constant.

Honestly, I don't write tests that use mock_model myself and am not intimately familiar with its history. I would probably avoid it unless you understand exactly why you're using it.

@myronmarston
Copy link
Member

This isn't really possible. Even if it were, the code under test wouldn't be able to use the constant.

I think it is possible if we change it to use stub_const. I believe that mock_model was written long before stub_const was conceived, and hasn't been revised in light of that new feature. I would agree with your suggestion ("I would probably avoid it unless you understand exactly why you're using it"), but I think we can improve it by using stub_const.

As it stands, right now it's "leaky" in the sense that it mutates the global constants and leaves them as such, which can cause ordering dependencies and other weirdness. stub_const would avoid these issues.

Regardless, this is really an rspec-rails issue, so perhaps this discussion should be moved there.

@alindeman
Copy link
Contributor

Interesting. Maybe rspec-rails 3.0 should leverage stub_const?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants