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?

On Feb 12, 2011, at 1:10 AM, Fearless F. wrote:

premise = double(“premise”, :id => 1)
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

David C. wrote in post #981239:

http://relishapp.com/rspec/rspec-rails/v/2-5/dir/mocks/mock-model
HTH,
David

Boy howdy, TH to the max. (How did I miss that???) Thanks.

  • ff