Skip to content

Extract stub_model/mock_model to a gem #894

Closed
alindeman opened this Issue Dec 27, 2013 · 12 comments

4 participants

@alindeman

I feel like using stub_model and mock_model are rarely a good idea. They are also really difficult to maintain since ActiveRecord seems to change in ways that break them nearly every time (currently they are broken on Rails 4.1-beta, which I'll address separately).

In the interest of discouraging their use, I propose extracting them to a gem in RSpec 3.0.0.

@thomas-holmes
RSpec member

Have you done any work on this yet? I've started tinkering with it. I'll reference something here when I've got it pushed.

@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-rails that referenced this issue Dec 29, 2013
@thomas-holmes thomas-holmes Remove mock_model and stub_model from rspec-rails
Some tests still fail without them as I have not yet figured out
a good way to fix them yet. View scaffold generators are also
currently broken.

rspec/rspec-rails#894
cf4b9a5
@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-activemodel-mocks that referenced this issue Dec 29, 2013
@thomas-holmes thomas-holmes A new gem that captures mock_model and stub_model.
This is a first pass extraction of the mock_model and
stub_model code. It doesn't have a lot of the supporting
configuration in it that other rspec projects currently
have and has not yet been stripped down to remove things
that are not necessary. I'm not happy with the need to
replicate the generation of a sample rails project but
it was the quickest way to get this off the ground for
now.

For rspec/rspec-rails#894
979cf8a
@alindeman

Haven't started yet. Looking forward to it, thanks :)

@thomas-holmes
RSpec member

I've pushed up the first set of changes. Still plenty to do. I should be getting more done this evening.

@thomas-holmes
RSpec member

I think this is at a pretty decent spot right now if you can find some time to review it. I don't want to do too much more without some feedback as it might be mostly unnecessary.
Modified rspec-rails:
https://github.com/thomas-holmes/rspec-rails/tree/remove-mocks

New gem repo:
https://github.com/thomas-holmes/rspec-activemodel-mocks

(Fixed the new gem repo link, I renamed it)

@myronmarston
RSpec member

Does your new gem actually depend on rails? Or simply activerecord? Or maybe even just activemodel?

If it's one of the latter, it would be good to name the gem rspec-activerecord-mocks or rspec-activemodel-mocks.

@thomas-holmes
RSpec member

Thanks for the suggestion. I'm not completely certain, I'll need to reread through the actual mocking code. At a quick glance it looks like it relies on ActiveRecord and ActiveSupport.

I'll rename it if that's the case once I've verified.

@thomas-holmes
RSpec member

A lot of its tests will have to be rewritten (they were being run inside the context of a rails app) but I don't see any aspect of the functionality the gem itself provides that requires a full rails stack. I'll start modifying the gem/tests to remove its dependency on Rails.

@thomas-holmes
RSpec member

I've got the gem now pared down to only having explicit runtime dependencies on ActiveModel and ActiveSupport. I've also renamed the repo and the gem to rspec-activemodel-mocks.

I'm not really happy with the testing situation, it feels pretty hackish but at least it's working. I have some ideas for improving the tests in a way that a database might not be required but I'm not sure yet if that will pan out.

https://github.com/thomas-holmes/rspec-activemodel-mocks

@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-rails that referenced this issue Jan 5, 2014
@thomas-holmes thomas-holmes Deprecate `mock_model` and `stub_model` 72c4afb
@alindeman alindeman added a commit that referenced this issue Jan 5, 2014
@thomas-holmes thomas-holmes Remove mock_model and stub_model from rspec-rails
Some tests still fail without them as I have not yet figured out
a good way to fix them yet. View scaffold generators are also
currently broken.

rspec/rspec-rails#894
0357278
@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-rails that referenced this issue Jan 16, 2014
@thomas-holmes thomas-holmes Remove mock_model and stub_model from rspec-rails
Some tests still fail without them as I have not yet figured out
a good way to fix them yet. View scaffold generators are also
currently broken.

rspec/rspec-rails#894
805b916
@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-rails that referenced this issue Jan 16, 2014
@thomas-holmes thomas-holmes Remove mock_model and stub_model from rspec-rails
Some tests still fail without them as I have not yet figured out
a good way to fix them yet. View scaffold generators are also
currently broken.

rspec/rspec-rails#894
8170eb3
@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-rails that referenced this issue Jan 16, 2014
@thomas-holmes thomas-holmes Deprecate `mock_model` and `stub_model` 5e1da9b
@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-rails that referenced this issue Feb 9, 2014
@thomas-holmes thomas-holmes Deprecate `mock_model` and `stub_model` 5b62cef
@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-rails that referenced this issue Feb 9, 2014
@thomas-holmes thomas-holmes Deprecate `mock_model` and `stub_model` 0eaf079
@thomas-holmes thomas-holmes added a commit to thomas-holmes/rspec-rails that referenced this issue Feb 10, 2014
@thomas-holmes thomas-holmes Deprecate `mock_model` and `stub_model` c73d95d
@jezstephens

Hi guys. In what sense is using these features "discouraged"? If the goal is to discourage people from adding the new gem en masse, I think some guidance is necessary. It is not clear to me what is considered an appropriate use case for these features.

Though I use plain doubles or OpenStructs where I can, I often find myself using mock_model to avoid raising ActiveRecord::AssociationTypeMismatch when assigning to an association. I am not aware of a simple alternative but I would be very happy to hear one. This must be a very common use case.

Perhaps something can be added to the upgrade guide?

@thomas-holmes
RSpec member

I agree, some more guidance would be good. The idea is that doubles can often fill the role during isolated unit tests and real instances should work fine in larger integration style tests. If you are aware of any other situations in which you feel their use is necessary that would be great information to have.

@jezstephens

The problem as I see it is that Rails makes isolated testing a bit difficult when it comes to models and controllers because they are so tied up with ActiveRecord and ActionController. Sometimes we have to compromise, and mock_model can make this a lot easier.

I hate to throw a spanner in the works, but I wonder if it might be better to deprecate only stub_model. I have not personally found a use for that one. Any thoughts?

@alindeman alindeman added a commit that referenced this issue Mar 3, 2014
@thomas-holmes thomas-holmes Remove mock_model and stub_model from rspec-rails
Some tests still fail without them as I have not yet figured out
a good way to fix them yet. View scaffold generators are also
currently broken.

rspec/rspec-rails#894
29c1758
@alindeman

Fixed with #913 and #902

@alindeman alindeman closed this Mar 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.