Mocha with Activerecord?

350 views
Skip to first unread message

Pito

unread,
Jun 22, 2010, 1:21:34 PM6/22/10
to mocha-developer
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.

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

Duncan Beevers

unread,
Jun 25, 2010, 12:05:30 PM6/25/10
to mocha-d...@googlegroups.com
If what you expect is to be able to retrieve an object instance from
an association, mock that point of the interaction.

a_card = Card.new(:name => "Joe Winston")

a_phone = mock()
a_card.expects(:home_phone).returns(a_phone)

So I don't know what you're trying to test, but simply testing
ActiveRecord is generally considered bad form.

If you want to test that the association just accepts and returns a
phone number, go ahead and use a real phone number object instead of a
mock.

> --
> You received this message because you are subscribed to the Google Groups
> "mocha-developer" group.
> To post to this group, send email to mocha-d...@googlegroups.com.
> To unsubscribe from this group, send email to
> mocha-develop...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mocha-developer?hl=en.
>
>

--
Sent from my mobile device

Pito Salas

unread,
Jun 25, 2010, 12:14:45 PM6/25/10
to mocha-d...@googlegroups.com
Hi Duncan

Thanks... I wasn't testing ActiveRecord, as much as wanting to prove
that I had properly configured all my associations. I have a pretty
complicated set of associations. The case I reported in my question
was kind of reducing the problem to it's minimum to make it clear.

So: are you saying that it's not possible or hard to create a mock
with mocha which an active record associations would accept in an
assignment, i.e.

model parent has_many :children records
c = mock object stands in for a 'child record'
m = instance of 'parent'

m.children << c

Requires that when << sees 'c' it allows it to be added to the
'children' association. What can I do to mock 'c' so that the <<
works?

Impossible? (I know, I can create a 'real' Child and it will work)

Thanks!

Pito

Reply all
Reply to author
Forward
0 new messages