Problems using Mocha plugin with Rails 3

119 views
Skip to first unread message

Jack Zelig

unread,
Oct 10, 2011, 10:43:26 AM10/10/11
to mocha-developer
Hi,

I'm trying to use the Mocha plugin in conjunction with the Rails test
framework.

I understand that Mocha must be loaded after the test framework, but I
cannot seem to get this to happen.

I have ensured that the content of "vendor/plugins/mocha/init.rb" is
commented out and I then require Mocha in my test_helper.rb, thus:
...
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'mocha'
class ActiveSupport::TestCase
...

However, when I run my tests I get the error:
... active_support/test_case.rb:14: warning: already initialized
constant Mocha
rake aborted!

Upon further inspection, it seems that despite an empty "init.rb"
Rails is still loading Mocha before its own test framework, and this
is what is causing the error.

I believe that "vendor/plugins/mocha/lib/mocha.rb" is to blame, as
this path is also automatically loaded, too.

I've tried ensuring that Mocha isn't loaded by editing application.rb
to exclude the plugin (i.e. config.plugins =
[ :everything_except_mocha ]), but when I come to then require Mocha I
get a "No such file to load" error.

rails: 3.0.3
ruby: 1.9.2
mocha: 0.10.0

Can someone give me a clue as to how I can get the Mocha plugin
working with Rails 3?

Thanks very much in advance!

Jack

P.S. I tried installing the Mocha gem and that works great. The only
problem is that for this project the client has an aversion to gems or
anything else he must install on his machine (genuinely!!)
He still however wants to be able to run the tests himself.

Fernando Guillen

unread,
Oct 10, 2011, 1:12:11 PM10/10/11
to mocha-d...@googlegroups.com
On Mon, Oct 10, 2011 at 4:43 PM, Jack Zelig <jack....@googlemail.com> wrote:
> However, when I run my tests I get the error:
> ... active_support/test_case.rb:14: warning: already initialized
> constant Mocha
> rake aborted!

How are you declaring the 'mocha' gem in the Gemfile?

Have you remembered to use ':require => false' ?

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

f.


--
Fernando Guillén
Freelance Web Developer
http://www.fernandoguillen.info
http://spainrb.org/fernando-guillen

Jack Zelig

unread,
Oct 10, 2011, 2:47:37 PM10/10/11
to mocha-d...@googlegroups.com
On 10.10.2011 19:12, Fernando Guillen wrote:
>> > However, when I run my tests I get the error:
>> > ... active_support/test_case.rb:14: warning: already initialized
>> > constant Mocha
>> > rake aborted!
> How are you declaring the 'mocha' gem in the Gemfile?
>
> Have you remembered to use ':require => false' ?
>
> group :test do
> gem 'mocha', :require => false
> end
I'm trying to get Mocha to work as a plugin, without it being installed
on my machine as a gem.
Do I have to declare plugins in the gem file?

James Mead

unread,
Oct 10, 2011, 3:57:50 PM10/10/11
to mocha-d...@googlegroups.com
Hi Jack,

I haven't tried using Mocha as a plugin for ages.

Is there a particular reason you want to use it as a plugin rather than a gem?

I'll have a quick look into why it might not be working.

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

> --
> 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.
>

Jack Zelig

unread,
Oct 11, 2011, 3:56:48 AM10/11/11
to mocha-d...@googlegroups.com
Hi James,

Thanks for the reply.
The bloke I am working for at the moment doesn't want me to use any gems
on the project.
He cites a reluctance to be dependent on 3rd party code as a reason.
He will however sanction the use of plugins as they are somewhat more
'contained', i.e. local to the project as opposed to being accessible
system-wide.
As you can imagine, this doesn't make my life easier, but c'est la vie!

Anyway, I would be really grateful if you could look into this for me.
As I wrote, it seems that Rails is loading
vendor/plugins/mocha/lib/mocha.rb before its own test framework.

Thanks very much for your help
Jack

James Mead

unread,
Oct 11, 2011, 4:23:45 AM10/11/11
to mocha-d...@googlegroups.com
Hi Jack,

In Rails 3, I can't think of any advantage of using Mocha as a plugin rather than a gem. To avoid a dependency on system-wide gems (an understandable requirement), I think the best solution is to use Bundler and specify a path local to the project e.g. `bundle install --path vendor/bundle`. This will keep gems for the project installed separately from system-wide gems and subsequent calls to Bundler commands will remember the chosen path. See [1].

I hope this helps. I'll try to find some time to investigate whether Mocha can still work as a plugin, but I'm pretty busy at the moment and to be honest it's not going to be a high priority.

Regards, James.

[1] http://gembundler.com/bundle_install.html

Jack Zelig

unread,
Oct 11, 2011, 4:53:34 AM10/11/11
to mocha-d...@googlegroups.com
Hi James,
Thanks for the link. I have just been reading through it. However, it
seems to me that running

`bundle install --path vendor/bundle`

will install all of the projects gems to vendor/bundle.
I'll have to keep on reading/researching to see if there is a way to
install only one gem to the vendor folder, using bundler.

Thanks for your help so far.
I would still be grateful if you let me know if you manage to make Mocha
work as a plugin.

Cheers,
Jack

Carl Tanner

unread,
Oct 11, 2011, 12:38:55 PM10/11/11
to mocha-d...@googlegroups.com
If you use bundler and rvm, the gem will not be system wide, but they will just apply to the project. On the other hand, more difficulty mean more work, means , more money.

Carl
@captproton

James Mead

unread,
Oct 11, 2011, 1:53:20 PM10/11/11
to mocha-d...@googlegroups.com
On 11 Oct 2011, at 09:53, Jack Zelig wrote:

> Thanks for the link. I have just been reading through it. However, it seems to me that running
>
> `bundle install --path vendor/bundle`
>
> will install all of the projects gems to vendor/bundle.

You are correct. It will indeed install *all* of the project's gems to the specified directory, but since the bloke you are working for doesn't want to rely on "system-wide" gems, I thought it would be preferable to install all of them locally within the project. Since it sounds like you have other gems in the Gemfile, why is it ok for them to be "system-wide"? What is so special about Mocha that is must not be "system-wide" when other gems can be?

I may be missing something, but it seems to me that the bloke you are working for is being a bit unreasonable.

> I'll have to keep on reading/researching to see if there is a way to install only one gem to the vendor folder, using bundler.

I don't think there is a way to do that - at least not in a way that would be at all simple.

> Thanks for your help so far.
> I would still be grateful if you let me know if you manage to make Mocha work as a plugin.

I'll try to find some time to look into this, but if it turns out it's difficult to make it work, I think I'm more likely to withdraw the ability to use Mocha as a plugin, since I don't see that there are any real advantages versus using it as a gem.

James Mead

unread,
Oct 11, 2011, 1:55:08 PM10/11/11
to mocha-d...@googlegroups.com
On 11 Oct 2011, at 17:38, Carl Tanner wrote:

> If you use bundler and rvm, the gem will not be system wide, but they will just apply to the project. On the other hand, more difficulty mean more work, means , more money.
>
> Carl
> @captproton

I think using RVM is a red herring. You could use an RVM gemset, but to my mind using Bundler with a local path for gems is a simpler solution. Especially when you start to think about deploying the app.

Jack Zelig

unread,
Oct 12, 2011, 4:33:19 AM10/12/11
to mocha-d...@googlegroups.com

> I may be missing something, but it seems to me that the bloke you are working for is being a bit unreasonable.

No, you're not missing anything. :-)

> I'll try to find some time to look into this, but if it turns out it's difficult to make it work, I think I'm more likely to withdraw the ability to use Mocha as a plugin, since I don't see that there are any real advantages versus using it as a gem.

I appreciate that. Even a "Sorry, it doesn't work as a plugin" would
help, as then I would have a case for being able to use it as a gem
after all.

Other than that, I have installed Mocha as a gem and am enjoying using it.
Thanks for a great tool!

Jack

Reply all
Reply to author
Forward
0 new messages