The basic question: how do you mock (or stub) a reference to a
polymorphic class in RSpec? Trying the obvious thing leads to an error
of the form:
undefined method `base_class'' for Post:Class
=== The details:
# file: app/models/relation.rb
class Relation < ActiveRecord::Base
belongs_to :entity, :polymorphic => true
end
# file: db/xxx_create_relations.rb
class CreateRelations < ActiveRecord::Migration
def change
create_table :relations do |t|
t.references :entity, :polymorphic => true, :null => false
end
end
end
# file: spec/models/relation_spec.rb
describe Relation do
it ''should create instance without error'' do
post = mock_model("Post")
lambda { Relation.create!(:entity => @post)}.should_not raise_error
end
end
=== Running the test:
1) Relation should create instance without error
Failure/Error: lambda { Relation.create!(:entity => post)
}.should_not raise_error
expected no Exception, got #<NoMethodError: undefined method
`base_class'' for Post:Class>
# ./spec/models/relation_spec.rb:39:in `block (2 levels) in <top
(required)>''
My hunch is that ActiveRecord is calling base_class on the mocked model
(Post) to determine what to stick in the ''entity_type'' column
in the
relations table. I tried explicitly setting it, as in:
post = mock_model("Post", :base_class => "Post")
but the error was the same.
For all the reasons that DC espouses, I''d love to mock (or stub)
references to the polymorphic models, but I don''t see how to do that.
Any ideas?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
David Chelimsky
2012-Mar-29 13:00 UTC
Re: rspec: mocking a reference to a polymorphic model?
On Thu, Mar 22, 2012 at 7:23 AM, Fearless Fool <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> The basic question: how do you mock (or stub) a reference to a > polymorphic class in RSpec? Trying the obvious thing leads to an error > of the form: > > undefined method `base_class'' for Post:Class > > === The details: > > # file: app/models/relation.rb > class Relation < ActiveRecord::Base > belongs_to :entity, :polymorphic => true > end > > # file: db/xxx_create_relations.rb > class CreateRelations < ActiveRecord::Migration > def change > create_table :relations do |t| > t.references :entity, :polymorphic => true, :null => false > end > end > end > > # file: spec/models/relation_spec.rb > describe Relation do > it ''should create instance without error'' do > post = mock_model("Post") > lambda { Relation.create!(:entity => @post)}.should_not raise_error > end > end > > === Running the test: > > 1) Relation should create instance without error > Failure/Error: lambda { Relation.create!(:entity => post) > }.should_not raise_error > expected no Exception, got #<NoMethodError: undefined method > `base_class'' for Post:Class> > # ./spec/models/relation_spec.rb:39:in `block (2 levels) in <top > (required)>'' > > My hunch is that ActiveRecord is calling base_class on the mocked model > (Post) to determine what to stick in the ''entity_type'' column in the > relations table. I tried explicitly setting it, as in: > > post = mock_model("Post", :base_class => "Post") > > but the error was the same. > > For all the reasons that DC espouses, I''d love to mock (or stub) > references to the polymorphic models, but I don''t see how to do that. > Any ideas?I don''t espouse stubbing or mocking associations, as it requires knowledge of Rails internals (as in your example). I do espouse stubbing domain-specific APIs on models in controller specs, because that provides fine-grained control of state and decoupling from the database, but that is an entirely different matter/context. HTH, David -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.