Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


undefined method `stub' for Model #310

jfelchner opened this Issue · 11 comments

8 participants


David, I'm attempting to mock using Mocha and I've been shaving yaks all day trying to get this figured out. :)

I'd like to create #stub_model in a gem and have it work for Mocha as it does for RSpec.

The issue I'm having is that because rspec/rails/mocks is being required at all times from rspec/rails, a problem arises when mocking with Mocha. There is RSpec-specific code in rspec/rails/mocks (in particular the calls to #stub on the ActiveModel object) and a
undefined method `stub' for Model
error is returned consistently when used.

Please let me know if I'm thinking incorrectly about this, but based on the other code centered around modularizing the mocking framework, it seems as though if you config.mock_with :mocha, #stub_model and #mock_model should not be available to be called.

In a related note, one would assume that even if rspec/rails/mocks was getting called, I should be able to config.include my own library and override the built-in #stub_model call. Unfortunately this seems to not be working either.

Even though I've made certain my library is getting loaded (through debug statements) it will not call my #stub_model instead of RSpec's. (This is quite possibly a fault of mine and not the purpose of this ticket. If you have any ideas though, I'd be all ears. :)

If I comment out the require 'rspec/rails/mocks' line in rspec/rails, not only does the 'stub' error go away (obviously) but it also loads my library correctly and everything seems to be working as expected.


David, I just started taking a crack at issue #310 and had a couple questions.

I wanted to write a test like this: as just a first pass to get the test to fail. It failed and I got it to pass. I also was able to keep all the other tests green... when run in isolation. As soon as I run rake however, half the tests fail.

I'm assuming this is due to load order and the environment not resetting itself after each test run.

The first pass solution I came up with was simply to do this:

However this feels very hacky to me.

This solution also has the immediate problem that rspec/rails cannot be required prior to the mocking framework being defined (as it currently is in spec_helper).

This leads me to believe that (for now) the proper place for this logic is in rspec-core where all the other mock file requirements are happening.

There is one more reason I believe this needs to be in rspec-core; Because of Rails 3's more modular nature, ActiveMode/ActiveRecord can more easily be used outside of Rails. Since #stub_model and #mock_model affect AM/AR objects, I feel that having it in rspec-rails is limiting its use.

What are your thoughts on this?


Any updates on this? I'd be happy to fix it but just need an idea on the direction you'd like the solution to take.


@jfelchner - I think it's fine to only require rspec/rails/mocks if rspec/mocks is loaded. Something like if defined?(RSpec::Mocks).


David, sorry for keeping this hanging out here for so long. I completely forgot about it.

I made a commit here: jfelchner/rspec-rails@2c99e92

Which accomplishes the above solution. However there's a secondary problem.

It seems that because of the fact that #mock_with can get called after require rspec/rails in spec_helper, when the require happens, rspec-rails has not yet loaded RSpec::Mocks (which is correct IMO) but because it hasn't loaded RSpec::Mocks, we can't check to see if we should load rails/mocks (which is specific to RSpec's mocking framework).

Again, I think this means we should do the reverse and rspec-core or rspec-mocks should check to see if rspec-rails is loaded and if so, require rails/mocks

You can argue it both ways. Should rspec-rails be responsible for mocks getting loaded? Should rspec-mocks be responsible for AR/AM specific code getting loaded? Either way we go it's a gray area but the cross-cutting concerns seem to be pointing to the latter option.

Can you think of something I'm missing?


Definitely don't want rspec-core or rspec-mocks knowing about rspec-rails.

We just need a means of including RSpec::Rails::Mocks late. Let me give that some thought.


Hi, any news about this one?


@dchelimsky, any thoughts here? I can dig too, but it seemed like you might have a few thoughts already. Would love to see this one fixed and/or closed!


@alindeman is this still an issue. I might be able to dig next week or so.


@dchelimsky, @samphippen, any news on this?


I'm getting this without trying to use mocha.


Closing as this is stale. The previous provided AR mocks have been moved into a separate gem: rspec-activemodel-mocks

@cupakromer cupakromer closed this
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.