Mocking leaked to other tests in Sinatra and Rspec

608 views
Skip to first unread message

Noppanit

unread,
Feb 10, 2012, 4:30:11 AM2/10/12
to mocha-developer
Hi,

first of all, I really love mocha (coffee and this). But I'm having
this problem quite often now. I know that there are some thread on
google that in Rails you might find mocha leaks to other tests and
causing unexpected invocation in other tests. I have that problem as
well but in Rspec and Sinatra. So, If I run the whole suite I get lots
of errors but if I run the test individually the tests are ok.

I put

gem 'mocha', :require => false into my Gemfile already

and I also put require 'mocha' at the end of my spec_helper.rb

but I still have that problem. Not sure anything else I could try
here. Or can I force mocha to tear down in after(:each) method?

Best,

James Mead

unread,
Feb 10, 2012, 4:49:23 AM2/10/12
to mocha-d...@googlegroups.com
I'm sorry to hear that. Can you tell me what version of Ruby, Rspec and Mocha you are using?

Is there any way you can extract a couple of tests that exhibit this problem so I can reproduce it locally?

Can you try running the specs with the Mocha debug option switched on?

MOCHA_OPTIONS=debug rake

This should output some debug statements as Mocha is loaded before the specs are run. Can you post the output here?

Is the problem always an unexpected invocation?

Thanks, James.

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

James Mead

unread,
Feb 10, 2012, 6:45:52 AM2/10/12
to mocha-d...@googlegroups.com
Also if you're using Mocha v0.10.2 or v0.10.3, can you try v0.10.4 (just released) and see whether that fixes your problem?

Thanks, James.

On 10 Feb 2012, at 09:30, Noppanit wrote:

Noppanit

unread,
Feb 10, 2012, 7:59:21 AM2/10/12
to mocha-developer
Hi,

I upgraded Mocha to 0.10.4 already and I'm using ruby 1.8.7, RSpec
2.8.0. And the problem still persists. I export MOCHA_OPTIONS=debug
already but it's not showing anything on the screen, just the error
unexpected invocation.

Noppanit

unread,
Feb 10, 2012, 8:04:29 AM2/10/12
to mocha-developer
But if I put these two statements at after(:each) method it runs fine.

Mocha::Mockery.instance.teardown
Mocha::Mockery.reset_instance

James Mead

unread,
Feb 10, 2012, 8:20:05 AM2/10/12
to mocha-d...@googlegroups.com
On 10 Feb 2012, at 12:59, Noppanit wrote:

I upgraded Mocha to 0.10.4 already and I'm using ruby 1.8.7, RSpec
2.8.0. And the problem still persists.

Thanks. I've tried reproducing your problem in this gist [1], but every seems to work ok for me. Does this gist look representative of what you are doing? If not can you suggest specific changes that would make it more representative? I think we need to try and extract an example of the failure in your specs into something I can reproduce locally. I'd be really grateful if you could try to do this for me.

I export MOCHA_OPTIONS=debug
already but it's not showing anything on the screen, just the error
unexpected invocation.

Sorry. This was my fault. I forgot that Rspec integrates with Mocha in it's own way and therefore this environment variable will have no effect. It only works with Test::Unit and MiniTest.

Cheers, James.

James Mead

unread,
Feb 10, 2012, 8:27:12 AM2/10/12
to mocha-d...@googlegroups.com
On 10 Feb 2012, at 09:30, Noppanit wrote:

I put

gem 'mocha', :require => false into my Gemfile already

and I also put require 'mocha' at the end of my spec_helper.rb

This is actually a red herring. Doing this is only necessary when using Test::Unit and MiniTest, because they must be loaded before Mocha so that Mocha can monkey-patch them. Rspec integrates more sensibly with Mocha and so doesn't need this monkey-patching.

I've updated this gist [1] to make this more obvious.

James Mead

unread,
Feb 10, 2012, 8:31:51 AM2/10/12
to mocha-d...@googlegroups.com
On 10 Feb 2012, at 13:04, Noppanit wrote:

> But if I put these two statements at after(:each) method it runs fine.
>
> Mocha::Mockery.instance.teardown
> Mocha::Mockery.reset_instance

This confirms that the problem is that Mocha's teardown code is not being called by Rspec. But I wouldn't want you to have to keep that in your after(:each).

Can you confirm that you have configured Rspec to use Mocha?

RSpec.configure do |config|
config.mock_framework = :mocha
end

Also can you tell us what else is in your bundle? Perhaps some other library is interfering…?

Cheers, James.

David Chelimsky

unread,
Feb 10, 2012, 8:46:05 AM2/10/12
to mocha-d...@googlegroups.com

I hadn't seen James' message (above) about configuring RSpec, so if
that solved it for you, great, but if not ...

This is a complete longshot (since I can't reproduce the issue
myself), but rspec aliases mocha's setup, verify, and teardown methods
using alias (not alias_method) instead of delegating to them. The
`alias` method creates a new pointer to the same method body, so if
the aliased method is replaced by a new implementation (perhaps
because a file is reloaded), the alias is still pointing to the old
one.

1.9.3-p0 :001 > def foo; "foo"; end
=> nil
1.9.3-p0 :002 > alias bar foo
=> nil
1.9.3-p0 :003 > bar
=> "foo"
1.9.3-p0 :004 > def foo; "FOO"; end
=> nil
1.9.3-p0 :005 > foo
=> "FOO"
1.9.3-p0 :006 > bar
=> "foo"

I just posted a commit to a mocha-integration-issue branch in
rspec-core that defines methods that delegate to mocha instead of
aliasing mocha's methods. Please point your Gemfile at rspec-core on
github like this:

gem "rspec-core", :git => "git://github.com/rspec/rspec-core/",
:branch => "mocha-integration-issue"

If that solves the problem I'll merge this into master and release it
in the 2.9 release.

Cheers,
David

Noppanit

unread,
Feb 10, 2012, 6:47:32 PM2/10/12
to mocha-developer
source :rubygems




gem 'sinatra'
gem 'sinatra-contrib'
gem 'activesupport'
gem 'i18n'
gem 'neography'
gem 'haml'
gem 'oauth'
gem 'facebook_oauth'
gem 'twitter_oauth'
gem 'json'
gem 'flickraw'
gem 'soundcloud'
gem 'nokogiri'
gem 'dalli'
gem 'require_all'
gem 'rmagick'
gem 'aws-s3'
gem 'pony'
gem 'rack-flash'
gem 'sinatra-flash'
gem 'uuid'
gem 'resque'

group :development, :test do
gem 'system_timer'
gem 'rack-perftools_profiler'
gem 'perftools.rb'
gem 'stackdeck'
gem 'cucumber'
gem 'cucumber-sinatra'
gem "rack-test", "0.6.1"
gem "capybara", "~> 1.1.2"
gem "selenium-webdriver", "~> 2.18.0"
gem 'launchy'
gem 'rspec', "~> 2.8.0"
gem "rspec-core", :git => "git://github.com/rspec/rspec-
core.git", :branch => "mocha-integration-issue"
gem 'jasmine'
gem 'mocha', "~> 0.10.4", :require => false
end

Here is my Gemfile I put the new rspec-core in that already but I'm
still getting that error.

On Feb 10, 1:46 pm, David Chelimsky <dchelim...@gmail.com> wrote:

Noppanit

unread,
Feb 10, 2012, 6:59:57 PM2/10/12
to mocha-developer
Do I need to include this

RSpec.configure do |config|
config.mock_framework = :mocha
end

in every spec file?

David Chelimsky

unread,
Feb 10, 2012, 7:13:34 PM2/10/12
to mocha-d...@googlegroups.com
On Fri, Feb 10, 2012 at 5:59 PM, Noppanit <noppa...@gmail.com> wrote:
> Do I need to include this
>
>  RSpec.configure do |config|
>      config.mock_framework = :mocha
>    end
>
> in every spec file?

The convention is to put that in 'spec/spec_helper.rb', and then
require that file from every spec file.

Alan

unread,
Jul 3, 2012, 5:12:28 AM7/3/12
to mocha-d...@googlegroups.com
Any news on this?

I just found that my mocha expectation are leaking between rspec examples.

Reseting mocha with after(:each) fixes the problem. However I couldn't get your branch with the potential fix because of incompatible versions (My rspec-rails expects ~> rspec-core 2.10.0 and the branch has 2.8.0). If you updated the branch I'll give it a go.

Alan

> To post to this group, send email to mocha-developer@googlegroups.com.
> To unsubscribe from this group, send email to mocha-developer+unsubscribe@googlegroups.com.

dchel...@gmail.com

unread,
Jul 4, 2012, 4:49:47 PM7/4/12
to mocha-d...@googlegroups.com
I just merged the fix to master, which is currently aligned w/ the 10.x series. 2.11 release coming soon will include this.

Cheers,
David
Reply all
Reply to author
Forward
0 new messages