NoMethodError: undefined method `mock' for #... with mocha 0.9.7

390 views
Skip to first unread message

Dmitry Maksimov

unread,
Jul 10, 2009, 2:16:39 PM7/10/09
to mocha-developer
Hi,

I`m using mocha with shoulda to test my rails application and when I
switched to 0.9.7 from 0.9.5, I began to receive errors with message
like in post title:
NoMethodError: undefined method `mock' for #...

I can't recognize the cause of this error, so I just switched back to
mocha 0.9.5

Has anybody faced same problem? How can I solve it?

James Mead

unread,
Jul 13, 2009, 7:00:47 AM7/13/09
to mocha-d...@googlegroups.com
Hi Dmitry,

The problem is almost certainly related to the load order of Test::Unit, Shoulda & Mocha.

As you can see from the 0.9.6 release notes [1], Mocha no longer loads any testing libraries itself - it now only patches libraries that have already been loaded.

I'm afraid I don't have any experience of using Shoulda, but if you can provide a simple failing test, I'm sure I could help.

Cheers, James.

[1] http://blog.floehopper.org/articles/2009/06/29/mocha-release-0-9-6


2009/7/10 Dmitry Maksimov <dmt...@gmail.com>



--
James.
http://blog.floehopper.org

James Mead

unread,
Jul 13, 2009, 7:17:24 AM7/13/09
to mocha-d...@googlegroups.com
I was thinking about this a bit more and it sounds quite like the problem described here [1].

So if you are using Shoulda and Mocha within a Rails project, I'd suggest doing something like this :-

  # config/environments/test.rb

  require 'test/unit' # add this line
  config.gem 'thoughtbot-shoulda', :lib => 'shoulda'

It's likely that the Rails commits associated with this [2] will fix the problem without you needing to change your app like this, but I'm not sure when they will be released.

Cheers, James.

[1] http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/50
[2] https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2858-mocha-release-096-heads-up


2009/7/13 James Mead <james...@gmail.com>

Ned Wolpert

unread,
Jul 13, 2009, 11:52:29 AM7/13/09
to mocha-developer
Just an fyi. I had the same problem where upgrading from 0.9.5->0.9.7
my test cases broke with the "undefined method `mock'" error, though
I'm not using 'Shoulda'. Adding the require listed below in my config/
environments/test.rb fixed it.

On Jul 13, 4:17 am, James Mead <jamesmea...@gmail.com> wrote:
> I was thinking about this a bit more and it sounds quite like the problem
> described here [1].
>
> So if you are using Shoulda and Mocha within a Rails project, I'd suggest
> doing something like this :-
>
>   # config/environments/test.rb
>
>   require 'test/unit' # add this line
>   config.gem 'thoughtbot-shoulda', :lib => 'shoulda'
>
> It's likely that the Rails commits associated with this [2] will fix the
> problem without you needing to change your app like this, but I'm not sure
> when they will be released.
>
> Cheers, James.
>
> [1]http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/50
> [2]https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2...
>
> 2009/7/13 James Mead <jamesmea...@gmail.com>

Dmitry Maksimov

unread,
Jul 14, 2009, 12:33:48 PM7/14/09
to mocha-developer
Hi,

thanks for reply. I fixed the problem, but noticed some issues.
If test-unit gem installed line
config.gem "test-unit", :lib => "test/unit" works.
If not just need to add
require 'test/unit'

I read the blog post before posted initial message, but did something
wrong... Need to be more attentive :)

--
Dmitry Maksimov
Reply all
Reply to author
Forward
0 new messages