# Mocking an ActiverRecord model

**URL:** <https://www.ruby-forum.com/t/mocking-an-activerrecord-model/190525>\
**Category:** Rails\
**Created:** [June 22, 2010, 4:26am UTC](https://www.ruby-forum.com/t/mocking-an-activerrecord-model/190525 "2010-06-22T04:26:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 22, 2010, 4:26am UTC](https://www.ruby-forum.com/t/mocking-an-activerrecord-model/190525/1 "2010-06-22T04:26:46Z")

</div>

In Rails 2.x, using Mocha, I was trying a pretty simple excercise,  
mocking an activerecord model, and an association.

I wrote something like this:

test “setting the home\_phone works” do  
a\_phone = mock()  
a\_card = Card.new(:name =\> “Joe Winston”)  
a\_card.home\_phone = a\_phone  
assert\_equal a\_phone, a\_card.home\_phone  
end

Card is specified to has\_one :home\_phone.

I got the error below the dashed line, which to me says that  
ActiveRecord is not recognizing the mocked variable a\_phone as a proper  
object to add to the association. I did a bunch of digging but could not  
figure out how to get the mock to work with Active Record.

Does anyone have a tip, it would be greatly appreciated! (p.s. I  
couldn’t find a mocha specific list, which is why I posted here.)

Thanks!

Pito

* * *

Exception: Phone(#2156481640) expected, got Mocha::Mock(#2149160840)  
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record/associations/association\_proxy.rb:262:in  
`raise_on_type_mismatch' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/belongs_to_association.rb:22:in`replace’  
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active\_record/associations.rb:1281:in  
`home_phone=' ./test/unit/card_test_with_mock.rb:14:in`test\_setting\_the\_home\_phone\_works’  
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active\_support/testing/setup\_and\_teardown.rb:62:in  
` __send__' /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/testing/setup_and_teardown.rb:62:in`run’  
/opt/local/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run' /opt/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in`each’  
/opt/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run' /opt/local/lib/ruby/1.8/test/unit/testsuite.rb:34:in`run’  
/opt/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each' /opt/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in`run’  
/opt/local/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in  
`run_suite' /mydev/.metadata/.plugins/org.rubypeople.rdt.testunit/ruby/RemoteTestRunner.rb:117:in`start\_mediator’  
/mydev/.metadata/.plugins/org.rubypeople.rdt.testunit/ruby/RemoteTestRunner.rb:62:in  
`start’  
/mydev/.metadata/.plugins/org.rubypeople.rdt.testunit/ruby/RemoteTestRunner.rb:332

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 22, 2010, 7:34am UTC](https://www.ruby-forum.com/t/mocking-an-activerrecord-model/190525/2 "2010-06-22T07:34:27Z")

</div>

On Jun 22, 3:26 am, Pito S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I got the error below the dashed line, which to me says that  
> ActiveRecord is not recognizing the mocked variable a\_phone as a proper  
> object to add to the association. I did a bunch of digging but could not  
> figure out how to get the mock to work with Active Record.

stub\_out is\_a? maybe ?

> Does anyone have a tip, it would be greatly appreciated! (p.s. I  
> couldn’t find a mocha specific list, which is why I posted here.)

[http://groups.google.com/group/mocha-developer](http://groups.google.com/group/mocha-developer)

Fred
