Mocking an ActiverRecord model

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:inreplace’
/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:intest_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:inrun’
/opt/local/lib/ruby/1.8/test/unit/testsuite.rb:34:in run' /opt/local/lib/ruby/1.8/test/unit/testsuite.rb:33:ineach’
/opt/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in run' /opt/local/lib/ruby/1.8/test/unit/testsuite.rb:34:inrun’
/opt/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in each' /opt/local/lib/ruby/1.8/test/unit/testsuite.rb:33:inrun’
/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:instart_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

On Jun 22, 3:26 am, Pito S. [email protected] 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

Fred