Lots of "unexpected invocation" errors with Mocha 0.9.10, Rails 3.0.3, Ruby 1.9.2

1246 views
Skip to first unread message

Jamie Phelps

unread,
Dec 23, 2010, 10:26:21 AM12/23/10
to mocha-developer
I'm working on upgrading an app to Rails 3 and right now our test
suite is erroring out a lot on unexpected invocations. These same
tests pass brilliantly under Mocha 0.9.8, Ruby 1.8.7, and Rails
2.3.10, so I'm thinking something might have changed with how Mocha
determines what invocations it expects. In the past I've been able to
use fixtures for anything I didn't stub out with Mocha, but that
doesn't seem to be the case now. If there's a place I should be RTFM,
a link would be great. I looked at the release notes for 0.9.9 and
0.9.10 but didn't see anything that seemed relevant.

Here's an example: http://pastie.org/1400736 I have a fixture object
for Discount with a code of 'a_code' so I'm not sure why Mocha is in
the mix here. Shouldn't it leave well enough alone and let my fixtures
handle things unless I say otherwise?

James Mead

unread,
Dec 23, 2010, 4:45:13 PM12/23/10
to mocha-d...@googlegroups.com

Hi Jamie,

I realise this must be very frustrating, but is there a way you can
narrow this down a bit more for us?

I think it would really help if you could try changing just one thing
at a time e.g. can you run the original version of the project with
Ruby 1.8.7 and Rails 2.3.10, but do just the Mocha upgrade from 0.9.8
to 0.9.10? Could you then also try upgrading just Rails from 2.3.10 to
3, but not upgrading Ruby?

Can you tell us some more details e.g. the exact version of Rails, the
exact version of Ruby, what test frameworks (and what versions) you
are using, and how you are requiring them...?

Ideally can you also try and send us a simple failing test (e.g. in a
fresh Rails project) that would allow us to reproduce the problem
locally...?

I'm afraid I'm away from home at the moment visiting family, so I'm
only going to be in sporadic internet contact.

Regards, James.
----
http://jamesmead.org/

Jamie Phelps

unread,
Dec 23, 2010, 6:48:02 PM12/23/10
to mocha-d...@googlegroups.com
I think it would really help if you could try changing just one thing
at a time e.g. can you run the original version of the project with
Ruby 1.8.7 and Rails 2.3.10, but do just the Mocha upgrade from 0.9.8
to 0.9.10?

Tests all pass with Mocha 0.9.10 under Ruby 1.8.7 and Rails 2.3.10.

Could you then also try upgrading just Rails from 2.3.10 to
3, but not upgrading Ruby?

Tests do not pass under Rails 3.0.3 with Ruby 1.8.7. For what it’s worth, Mocha 0.9.8 + Ruby 1.9.2 + Rails 3.0.3 still fails so it doesn’t appear to be a regression.


Can you tell us some more details e.g. the exact version of Rails, the
exact version of Ruby, what test frameworks (and what versions) you
are using, and how you are requiring them...?

Rails 3.0.3, Ruby 1.8.7-p302, Ruby 1.9.2-p0. Using a stock Test::Unit install for the respective Ruby versions. No third party testing like Rspec, Cucumber, etc.

Ideally can you also try and send us a simple failing test (e.g. in a
fresh Rails project) that would allow us to reproduce the problem
locally...?

I haven’t been able to reproduce the error in a trivial test app, but perhaps some more code would help? http://pastie.org/1401942 What is bothersome to me is that this test doesn’t have any Mocha code at all. The setup method does, but it’s on the StoreController itself and another class that is not involved in this test or the method being tested.

I hope this is helpful. Let me know if I can provide any more information.

/jp

=======================
Jamie Phelps, Code Wrangler
Agile Web Solutions

James Mead

unread,
Dec 24, 2010, 3:18:35 AM12/24/10
to mocha-d...@googlegroups.com

Hi Jamie,

That's all really helpful.

Do any of the Mocha expectations (unsatisfied or satisfied) listed in
the test failure correspond to expectations set up in *other* tests
using e.g. the #stubs or #expects methods? I'm wondering whether some
Mocha state is leaking between tests - perhaps because Mocha's
teardown hook is not being correctly fired.

Also can you run the tests with the following environment set...

MOCHA_OPTIONS=debug

...and supply us with the test output (or at least the first few lines).

This will give us some clues as to whether the Test::Unit
monkey-patching is working as expected.

Thanks, James.
----
http://jamesmead.org/

James Mead

unread,
Dec 24, 2010, 3:20:14 AM12/24/10
to mocha-d...@googlegroups.com

One other thing - do any of these tests pass when run on their own?
i.e. do they only fail when running the tests with rake? Or do they
fail however you run them?

James Mead

unread,
Dec 24, 2010, 1:15:11 PM12/24/10
to mocha-d...@googlegroups.com
Hi Jamie,

I've had a go at trying to reproduce [1] the problem you are seeing,
but with no luck so far.

I can successfully run functional tests using Mocha & fixtures with
Ruby 1.9.2-p0, Rails 3.0.3 & Mocha 0.9.10 in a fresh Rails project
generated using Rails 3.0.3. If you're upgrading a project from an
earlier version of Rails, I wonder whether some of the files generated
by the earlier version of Rails haven't been updated to be suitable
for the newer version of Rails.

I required Mocha, by adding this to my Gemfile :-

group :test do
gem 'mocha', '0.9.10', :require => false
end

And adding this to the bottom of my test_helper.rb :-

require 'mocha'

I used Bundler 1.0.7 for these tests. I also used SQLite - are you using MySQL?

I think it would be useful to see the setup code from your test just
to verify that it cannot be the culprit.

Regards, James.
----
[1] https://github.com/floehopper/foo

Jamie Phelps

unread,
Dec 24, 2010, 4:37:03 PM12/24/10
to mocha-d...@googlegroups.com
*facepalm* This is where the problem was! I wasn’t including :require => false in my Gemfile. Grrr… I’m both happy that the solution was so simple but I’m also irritated that I’ve wasted so much time (mine and yours) on this silly issue. ::sigh::

=======================
Jamie Phelps, Code Wrangler
Agile Web Solutions

James Mead

unread,
Dec 31, 2010, 11:24:42 AM12/31/10
to mocha-d...@googlegroups.com
Hi Jamie,

I'm glad you go this sorted out.

I've added a ticket [1] to try and make Mocha fail fast in this
scenario. I think I tried this once before, but I should have another
go or at least improve debugging and/or documentation.

Regards, James.
----
http://jamesmead.org/

[1] http://floehopper.lighthouseapp.com/projects/22289/tickets/79

Reply all
Reply to author
Forward
0 new messages