Test::Unit and Mocha (first project)

82 views
Skip to first unread message

Matt Smith

unread,
May 20, 2011, 2:20:49 AM5/20/11
to mocha-developer
Looking for some (hopefully) simple advice here. I am using Mocha with
Test::Unit in a Rails application, after coming from Rspec.

I have this line in one of my functional tests:
Organization.any_instance.stubs(:save).returns(false)

The test passes. All of my functional tests pass. My unit tests pass.
My integration tests pass. All of the tests pass when I run them with
rake. When I run them with autotest (this is the first round, when
auto test runs all tests), one of my integration tests which happens
to save an organization fails. If I save the integration tests, to
force it to run that test only, it passes. If I comment out the line
above with the stub, and run all the tests with autotest, the
integration test passes.

It seems that the stub is not being cleared between tests with
autotest. If I run 'rake', all tests pass. Is there a way to reset
stubs manually? Is this an issue? With Mocha? With Autotest? With me
and my tests?

I certainly can make the tests work without stubs/mocha... but I
figured I would ask the question anyway when I really need mocha!

Thank you for Mocha and I look forward to using it more!

Matt Smith

James Mead

unread,
May 20, 2011, 5:48:00 AM5/20/11
to mocha-d...@googlegroups.com
Hi Matt,

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

Matt Smith

unread,
May 20, 2011, 6:20:43 PM5/20/11
to mocha-developer
James,

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.

Thanks for your time!

Matt Smith

James Mead

unread,
May 21, 2011, 3:37:41 AM5/21/11
to mocha-d...@googlegroups.com
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.

Cool. Another difference might be that my "model" isn't an ActiveRecord model. I'll see if I can try that too.

Cheers, James.

James Mead

unread,
May 21, 2011, 10:44:23 AM5/21/11
to Mocha Developer
On 21 May 2011, at 08:37, James Mead wrote:

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

[1] https://gist.github.com/984564

Reply all
Reply to author
Forward
0 new messages