[rspec-users] mocking an AR: ActiveRecord::AssociationTypeMismatch error

David Chelimsky dchelimsky at gmail.com
Sat Feb 12 02:22:40 EST 2011

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?



More information about the rspec-users mailing list