[mocha-developer] Mocha Mock's hanging on after test run?

James Mead jamesmead44 at gmail.com
Wed Jan 17 06:26:23 EST 2007

On 17/01/07, Kevin Clark <kevin.clark at gmail.com> wrote:
> Hi guys,
> I'm running mocha (0.3.2) against Rails core and just found an issue
> where the mock doesn't go away after the test is run.
> For example:
>   def test_reset_bang_doesn_reinstall_named_routes
>     ActionController::Routing::Routes.named_routes.expects(:install).never
>     @session.reset!
>   end
>   def test_zzz
>     puts ActionController::Routing::Routes.named_routes.inspect
>     assert !ActionController::Routing::Routes.named_routes
>   end
> The print when test_zzz is run gives this:
> #<ActionController::Routing::RouteSet::NamedRouteCollection:0x14e0da8
> @mocha=#<Mocha::Mock:0x2333504 @__mock_name=nil,
> @expectations=[#<Mocha::Expectation:0x2333158 @count=0,
> @parameter_block=nil,
> @backtrace=["test/controller/integration_test.rb:146:in
> `test_reset_bang_doesn_reinstall_named_routes'",
> "/usr/local/lib/ruby/1.8/test/unit/testcase.rb:72:in `__send__'",
> "/usr/local/lib/ruby/1.8/test/unit/testcase.rb:72:in `run'",
> "/usr/local/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'",
> "/usr/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'",
> "/usr/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'",
> "/usr/local/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'",
> "/usr/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'",
> "/usr/local/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'",
> "/usr/local/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in
> `run_suite'",
> "/usr/local/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:67:in
> `start_mediator'",
> "/usr/local/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:41:in
> `start'",
> "/usr/local/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:29:in
> `run'", "/usr/local/lib/ruby/1.8/test/unit/autorunner.rb:200:in
> `run'", "/usr/local/lib/ruby/1.8/test/unit/autorunner.rb:13:in `run'",
> "/usr/local/lib/ruby/1.8/test/unit.rb:278",
> "test/controller/integration_test.rb:6"], @return_value=nil,
> @parameters=** any **, @invoked=4, @method_name=:install>],
> @stub_everything=false>, @module=#<Module:0x14e0d44>, @helpers=[],
> @routes={}>
> This is a big deal because this causes tests to fail in other places
> in the framework.
> Ideas?

Hi Kevin,

Sorry not to respond sooner - I've been away on holiday.

I've managed to reproduce the problem locally.

I haven't had a chance to investigate it thoroughly, but it looks like you
could fix it by patching the gem as follows...

Replace the comment on line #13 of instance_method.rb as follows...

    def unstub
      # intentionally empty


    def unstub

I'm not sure how this bug crept in, but the HEAD version of Mocha does not
seem to have the same bug.

We're really close to releasing a new version (0.4) of the gem, but if this
is causing you serious problems I will release a patched 0.3.3 version.

(Note that the patch above will break a couple of unit tests).

Let me know.


More information about the mocha-developer mailing list