Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

mock_model needs base_class? #606

avit opened this Issue Sep 11, 2012 · 6 comments


None yet
3 participants

avit commented Sep 11, 2012

In my specs I tried using a mock_model for checking ActiveRecord validation. The error I got was:

undefined method `base_class' for Dummy:Class

I assume this is an ActiveRecord class method. Should this be something that a mock_model responds to?

@dchelimsky dchelimsky closed this Sep 11, 2012

avit commented Sep 11, 2012

stub_model requires an existing class... Am I missing something?

# this works:
model = mock_model("Dummy", id: 123)
model.class.stub!(base_class: model.class)
Calendar.new(schedulable: model).should be_valid

mock_model's generated class already responds to :primary_key for the name of the model's id column, :base_class might be needed to satisfy both id and type for polymorphic associations...

@avit avit added a commit to avit/rspec-rails that referenced this issue Sep 11, 2012

@avit avit Give generated mock_model classes a base_class for polymorphic keys
Fixes #606

@dchelimsky dchelimsky reopened this Sep 11, 2012


dchelimsky commented Sep 11, 2012

I have very mixed feelings about this. See #435 for background.

There are two problems: first, the names stub_model and mock_model don't really convey the underlying intent to support ActiveModel API's exclusively with mock_model. The second is that this appears to be the second time that a change in Rails has resulted in the need to change mock_model because something is calling ActiveRecord APIs.

What if you could pass a String to stub_model and we build up a temp ActiveRecord class for you instead?

avit commented Sep 11, 2012

What if you could pass a String to stub_model and we build up a temp ActiveRecord class for you instead?

👍 That's what I thought the intent was. Agreed on mock_model vs. stub_model being confusing: but this being rspec-rails, I had assumed an instance of a "rails generate model" (i.e. an ActiveRecord double) rather than an ActiveModel.

@avit avit closed this Sep 11, 2012


alindeman commented Sep 12, 2012

@dchelimsky, do you mean that stub_model would act like mock_model if given a String?


dchelimsky commented Sep 12, 2012

@alindeman in terms of whether it uses a real class or generates one, yes, but it would still generate an ActiveRecord::Base subclass, whereas mock_model would continue to generate an ActiveModel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment