Fearless Fool
2012-Mar-22 17:27 UTC
[rspec-users] mocking a reference to a polymorphic model?
The basic question: how do you mock 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 references to the polymorphic models, but I don''t see how to do that. Any ideas? -- Posted via http://www.ruby-forum.com/.
Chris Habgood
2012-Mar-22 18:14 UTC
[rspec-users] mocking a reference to a polymorphic model?
Are you really sure you need a polymorphic table? On Thu, Mar 22, 2012 at 12:27, Fearless Fool <lists at ruby-forum.com> wrote:> The basic question: how do you mock 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 references to the > polymorphic models, but I don''t see how to do that. > > Any ideas? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- *"In matters of style, swim with the current; in matters of principle, stand like a rock." Thomas Jefferson * -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120322/633b8139/attachment.html>
Matt Wynne
2012-Mar-22 20:10 UTC
[rspec-users] mocking a reference to a polymorphic model?
On 22 Mar 2012, at 17:27, Fearless Fool wrote:> The basic question: how do you mock 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 references to the > polymorphic models, but I don''t see how to do that.Can you enumerate those reasons? This doesn''t seem like a good use-case for mock_model, to me. cheers, Matt -- Freelance programmer & coach Author, http://pragprog.com/book/hwcuc/the-cucumber-book Founder, http://www.relishapp.com/ Twitter, https://twitter.com/mattwynne -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120322/129836c0/attachment-0001.html>