I don't use autotest and I haven't heard about this problem before, so I can't offer any quick fixes.
It sounds like Mocha's teardown hook [1] is not getting called for some reason. You could temporarily try calling Mocha::API#mocha_teardown from within your test (that module should have been included into Test::Unit::TestCase) to see whether that makes a difference.
I've had a quick go at reproducing the problem in a blank Rails project, but without luck so far. Can you tell us what versions of everything you are using (i.e. Ruby, Rails, Autotest, Mocha, Test::Unit, etc)? Could you also indicate how you are requiring all the above? If you are using Bundler, can you include your Gemfile?
If you have time, could you have a go at creating a blank Rails project and try to reproduce the problem in the simplest way you can? i.e. only include the minimum number of gems and create a minimal set of tests that exhibit the problem. Here's my attempt so far :-
Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.0.7'
gem 'sqlite3'
gem 'autotest'
gem 'autotest-rails'
gem 'mocha', :require => false
----
app/models/foo.rb:
class Foo
def bar
"bar"
end
end
----
test/test_helper.rb:
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
fixtures :all
end
require "mocha"
----
test/unit/foo_test.rb:
require "test_helper"
class FooTest < Test::Unit::TestCase
def test_bar_stubbed
Foo.any_instance.stubs(:bar => "stubbed-bar")
foo = Foo.new
assert_equal "stubbed-bar", foo.bar
end
def test_bar_unstubbed
foo = Foo.new
assert_equal "bar", foo.bar
end
end
----
test/integration/foo_integration_test.rb:
require "test_helper"
class FooIntegrationTest < ActionController::IntegrationTest
def test_bar_stubbed
Foo.any_instance.stubs(:bar => "stubbed-bar")
foo = Foo.new
assert_equal "stubbed-bar", foo.bar
end
def test_bar_unstubbed
foo = Foo.new
assert_equal "bar", foo.bar
end
end
----
I run the tests using :-
bundle exec autotest -s rails
But I don't see the problem.
Regards, James.
----
[1] https://github.com/floehopper/mocha/blob/master/lib/mocha/api.rb#L159
> --
> 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.
>
> I will "mock this up", this coming weekend. I am not sure why your
> test did not mimic it. I don't have :require => false in my gem file.
Cool. Another difference might be that my "model" isn't an ActiveRecord model. I'll see if I can try that too.
Cheers, James.
> On 20 May 2011, at 23:20, Matt Smith wrote:
>
>> I will "mock this up", this coming weekend. I am not sure why your
>> test did not mimic it. I don't have :require => false in my gem file.
Hi Matt,
I've tried making the model into a proper ActiveRecord model, but that doesn't seem to make any difference.
After your recent comment, I tried removing the :require => false option from the Gemfile and the require "mocha" statement from the bottom of the test_helper.rb and I get the unstubbed tests to fail when running rake via bundle exec. However I don't see the same problems running autotest.
Hopefully you can see what I've been doing in this gist [1].
Adding the :require => false option stops Bundler auto-requiring Mocha so you can choose when it gets loaded. I usually require it at the bottom of test_helper.rb after all the other Test::Unit stuff has been loaded, because Mocha needs to monkey-patch Test::Unit. You can see this monkey-patching by enabling the Mocha debug option as shown at the bottom of the gist.
Cheers, James.