Fearless Fool
2011-Feb-12 03:10 UTC
[rspec-users] mocking an AR: ActiveRecord::AssociationTypeMismatch error
I have a Premise model with lots of validations and somewhat complex callbacks. I''ve already written tests for those (and they pass). I also have a MeteredService model for which premise :has_many metered_services and (of course) metered_service :belongs_to premise. Testing MeteredService doesn''t require a real Premise -- all it needs is a premise_id -- so for my MeteredService tests I tried a simple mock like: premise = double("premise", :id => 1) But this fails with: Failure/Error: MeteredService.create(:premise => @premise) ActiveRecord::AssociationTypeMismatch: Premise(#2202729700) expected, got RSpec::Mocks::Mock(#2154067120) I *really* don''t want to create a real Premise object unless there''s a way to inhibit all the validations and callbacks. (FWIW, I use FactoryGirl for other test, but it evidently triggers validations and callbacks just like an ordinary .create()) What are my options? -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2011-Feb-12 07:22 UTC
[rspec-users] mocking an AR: ActiveRecord::AssociationTypeMismatch error
On Feb 12, 2011, at 1:10 AM, Fearless Fool wrote:> I have a Premise model with lots of validations and somewhat complex > callbacks. I''ve already written tests for those (and they pass). > > I also have a MeteredService model for which premise :has_many > metered_services and (of course) metered_service :belongs_to premise. > > Testing MeteredService doesn''t require a real Premise -- all it needs is > a premise_id -- so for my MeteredService tests I tried a simple mock > like: > > premise = double("premise", :id => 1) > > But this fails with: > > Failure/Error: MeteredService.create(:premise => @premise) > ActiveRecord::AssociationTypeMismatch: > Premise(#2202729700) expected, got RSpec::Mocks::Mock(#2154067120) > > I *really* don''t want to create a real Premise object unless there''s a > way to inhibit all the validations and callbacks. (FWIW, I use > FactoryGirl for other test, but it evidently triggers validations and > callbacks just like an ordinary .create()) > > What are my options?http://relishapp.com/rspec/rspec-rails/v/2-5/dir/mocks/mock-model HTH, David
Fearless Fool
2011-Feb-12 16:14 UTC
[rspec-users] mocking an AR: ActiveRecord::AssociationTypeMismatch error
David Chelimsky wrote in post #981239:> http://relishapp.com/rspec/rspec-rails/v/2-5/dir/mocks/mock-model > HTH, > DavidBoy howdy, TH to the max. (How did I miss that???) Thanks. - ff -- Posted via http://www.ruby-forum.com/.