Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Mocking one test is mocking every next test
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Fernando Guillen  
View profile  
 More options Mar 14 2010, 2:24 pm
From: Fernando Guillen <fguillen.m...@gmail.com>
Date: Sun, 14 Mar 2010 19:24:29 +0100
Local: Sun, Mar 14 2010 2:24 pm
Subject: Mocking one test is mocking every next test

Hi people,

I have had a very hard headache with a behaivor I didn't spec.

Look this example unit test:

require 'test_helper'

class TestModelTest < ActiveSupport::TestCase
  def test_a
    assert( TestModel.new.valid? )
  end

  def test_b
    TestModel.any_instance.stubs(:valid?).returns(false)
    assert( !TestModel.new.valid? )
  end

  def test_c
    assert( TestModel.new.valid? )
  end
end

If I execute these tests one by one all of them work, but if I execute
all of them on a row the 'test_c' fails because the mock defined on
'test_b' is still working on 'test_c'.

You can reproduce this error on the minimal Rails project I attach.

~/Downloads $ unzip testing_mocha_bug.zip
~/Downloads $ cd testing_mocha_bug
~/Downloads/testing_mocha_bug $ rake db:migrate RAILS_ENV=test
~/Downloads/testing_mocha_bug $ ruby -Itest test/unit/test_model_test.rb
/Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:War ning:
Gem::Dependency#version_requirements is deprecated and will be removed
on or after August 2010.  Use #requirement
Loaded suite test/unit/test_model_test
Started
..F
Finished in 0.160461 seconds.

  1) Failure:
test_c(TestModelTest) [test/unit/test_model_test.rb:14]:
<false> is not true.

3 tests, 3 assertions, 1 failures, 0 errors

If I delete the line 'config.gem "mocha"' on the 'environment.rb' the
tests work fine, what is also a surprise for me because I'm not saying
on other place to 'require' the 'mocha' gem.

Any suggestions why does this behaivor is happening?.

Thanks

f.

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

  testing_mocha_bug.zip
132K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
floehopper  
View profile  
 More options Mar 14 2010, 5:05 pm
From: floehopper <jamesmea...@gmail.com>
Date: Sun, 14 Mar 2010 14:05:34 -0700 (PDT)
Local: Sun, Mar 14 2010 5:05 pm
Subject: Re: Mocking one test is mocking every next test
On Mar 14, 6:24 pm, Fernando Guillen <fguillen.m...@gmail.com> wrote:

Hi Fernando,

This is a load order problem. If you load Mocha using a config.gem
statement, you need to make sure that 'test/unit' has been loaded
first. You can do this by e.g. adding a require 'test/unit' before the
config.gem for Mocha. Alternatively you can change the config.gem for
Mocha to have the :lib => false option. This is similar to what
happens when you tried removing the Mocha config.gem line. In this
case, ActiveSupport::TestCase loads Mocha - and this works because
'test/unit' has already been loaded at this point.

I hope that helps.

Cheers, James.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fernando Guillen  
View profile  
 More options Mar 15 2010, 5:50 am
From: Fernando Guillen <fguillen.m...@gmail.com>
Date: Mon, 15 Mar 2010 10:50:48 +0100
Local: Mon, Mar 15 2010 5:50 am
Subject: Re: Mocking one test is mocking every next test
Hi James

On Sun, Mar 14, 2010 at 10:05 PM, floehopper <jamesmea...@gmail.com> wrote:
> This is a load order problem. If you load Mocha using a config.gem
> statement, you need to make sure that 'test/unit' has been loaded
> first. You can do this by e.g. adding a require 'test/unit' before the
> config.gem for Mocha. Alternatively you can change the config.gem for
> Mocha to have the :lib => false option. This is similar to what
> happens when you tried removing the Mocha config.gem line. In this
> case, ActiveSupport::TestCase loads Mocha - and this works because
> 'test/unit' has already been loaded at this point.

I glad this is an already known behaivor,

I think the most clean solution is to configure the environment.rb like you say:

    config.gem "mocha", :lib => false

> I hope that helps.

Indeed it was.

Thanks

f.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lucas Cavalcanti  
View profile  
 More options Mar 17 2010, 6:01 pm
From: Lucas Cavalcanti <lucasmrtu...@gmail.com>
Date: Wed, 17 Mar 2010 15:01:26 -0700 (PDT)
Local: Wed, Mar 17 2010 6:01 pm
Subject: Re: Mocking one test is mocking every next test
Hi James,

I'm having the same problem and I tried the solutions you pointed, but
nothing happened.
My stubs still holds on the subsequent tests.

I'm using test/unit and shoulda, and I don't have any config.gem
'mocha' on my environment.
My problematic stub is placed on a setup block.

Any thoughts?
Thanks
Lucas

On Mar 15, 6:50 am, Fernando Guillen <fguillen.m...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Mead  
View profile  
 More options Mar 18 2010, 5:45 am
From: James Mead <jamesmea...@gmail.com>
Date: Thu, 18 Mar 2010 09:45:43 +0000
Local: Thurs, Mar 18 2010 5:45 am
Subject: Re: Mocking one test is mocking every next test
On 17 March 2010 22:01, Lucas Cavalcanti <lucasmrtu...@gmail.com> wrote:

> I'm having the same problem and I tried the solutions you pointed, but
> nothing happened.
> My stubs still holds on the subsequent tests.

> I'm using test/unit and shoulda, and I don't have any config.gem
> 'mocha' on my environment.
> My problematic stub is placed on a setup block.

> Any thoughts?
> Thanks
> Lucas

How are you loading Mocha? Are you using it as a Rails plugin or do
you have it installed as a gem? Are you requiring 'mocha' anywhere or
are you relying on ActiveSupport::TestCase to load it for you? What
version of Ruby are you using? What version of Mocha are you using?

If you have a stub in a setup method, then it is intended behaviour
that it will exist for all the tests within that context. Or do you
mean that it's leaking into other contexts/testcases?

Can you show us an example like Fernando did?

Cheers, James.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lucas Cavalcanti  
View profile  
 More options Mar 18 2010, 9:26 am
From: Lucas Cavalcanti <lucasmrtu...@gmail.com>
Date: Thu, 18 Mar 2010 10:26:38 -0300
Local: Thurs, Mar 18 2010 9:26 am
Subject: Re: Mocking one test is mocking every next test

Hi,

How are you loading Mocha? Are you using it as a Rails plugin or do

> you have it installed as a gem? Are you requiring 'mocha' anywhere or
> are you relying on ActiveSupport::TestCase to load it for you?

I'm not requiring 'mocha' explicitly, I've installed it as a gem, and my
tests
extend ActiveSupport::TestCase

What

> version of Ruby are you using? What version of Mocha are you using?

 Ruby 1.8.7 and mocha 0.9.8

> If you have a stub in a setup method, then it is intended behaviour
> that it will exist for all the tests within that context. Or do you
> mean that it's leaking into other contexts/testcases?

It's leaking into other testcases.

> Can you show us an example like Fernando did?

class MyProblematicTest < ActionController::IntegrationTest
  context "Major context" do
    setup do
        @mock_memcache = mock()
        MemCache.stubs(:new).returns(@mock_memcache)
    end

    context "minor context" do
         setup do
              #some expectations on @mock_memcache
         end
         should "some assertion" do
              #....
         end
     end
  end

  class NextTestSuite < ActiveSupport::TestCase
     context "Cache is empty" do
        setup do
          MemCache.any_instance.stubs(:get).returns(nil)
        end

        should "fetch data" do
            MemCache.new(...).get(...) # unexpected invocation #Mock.....get
                                                    # MemCache.new returns
@mock_memcache,
                                                    # from the other
testcase, that is not a MemCache
                                                    # instance, and ignores
the stub from this setup
        end
     end
  end

Thanks, Lucas

Cheers, James.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lucas Cavalcanti  
View profile  
 More options Mar 18 2010, 9:50 am
From: Lucas Cavalcanti <lucasmrtu...@gmail.com>
Date: Thu, 18 Mar 2010 10:50:41 -0300
Local: Thurs, Mar 18 2010 9:50 am
Subject: Re: Mocking one test is mocking every next test

One more relevant info: This problem only occurs on autotest. When I run
with 'rake test' I can't
reproduce this leaking stubs behavior.

[]'s
Lucas
On Thu, Mar 18, 2010 at 10:26 AM, Lucas Cavalcanti
<lucasmrtu...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Mead  
View profile  
 More options Mar 18 2010, 1:22 pm
From: James Mead <jamesmea...@gmail.com>
Date: Thu, 18 Mar 2010 17:22:55 +0000
Local: Thurs, Mar 18 2010 1:22 pm
Subject: Re: Mocking one test is mocking every next test
On 18 March 2010 13:50, Lucas Cavalcanti <lucasmrtu...@gmail.com> wrote:

> One more relevant info: This problem only occurs on autotest. When I run
> with 'rake test' I can't
> reproduce this leaking stubs behavior.

How and where in your code are you loading Shoulda?

Thanks, James.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lucas Cavalcanti  
View profile  
 More options Mar 18 2010, 1:25 pm
From: Lucas Cavalcanti <lucasmrtu...@gmail.com>
Date: Thu, 18 Mar 2010 14:25:59 -0300
Local: Thurs, Mar 18 2010 1:25 pm
Subject: Re: Mocking one test is mocking every next test

> How and where in your code are you loading Shoulda?

on config/environments/test.rb:

config.gem 'thoughtbot-shoulda', :lib => 'shoulda/rails', :source => '
http://gems.github.com'

[]'s
Lucas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Mead  
View profile  
 More options Mar 18 2010, 2:10 pm
From: James Mead <jamesmea...@gmail.com>
Date: Thu, 18 Mar 2010 18:10:21 +0000
Local: Thurs, Mar 18 2010 2:10 pm
Subject: Re: Mocking one test is mocking every next test
On 18 March 2010 17:25, Lucas Cavalcanti <lucasmrtu...@gmail.com> wrote:

>> How and where in your code are you loading Shoulda?

> on config/environments/test.rb:

> config.gem 'thoughtbot-shoulda', :lib => 'shoulda/rails', :source =>
> 'http://gems.github.com'

I haven't had time to completely reproduce your environment and I
don't know much about autotest, but I'm pretty sure this is a
variation on this ticket [1]. The long and short of it is that I think
you need to make sure 'test/unit' is loaded before shoulda. Can you
try adding: require "test/unit" before the config.gem
"thoughtbot-shoulda". Alternatively if you are using the test-unit
gem, you should include a config.gem line for "test-unit" before the
config.gem "thoughtbot-shoulda".

I'm afraid I'm not going to be around much over the next week, so I'm
unlikely to be able to help for a bit, so I hope this works - or
someone else can help you.

Cheers, James.

[1] http://floehopper.lighthouseapp.com/projects/22289/tickets/50-after-m...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lucas Cavalcanti  
View profile  
 More options Mar 18 2010, 3:44 pm
From: Lucas Cavalcanti <lucasmrtu...@gmail.com>
Date: Thu, 18 Mar 2010 16:44:14 -0300
Local: Thurs, Mar 18 2010 3:44 pm
Subject: Re: Mocking one test is mocking every next test

the require 'test/unit' didn't work, and the config.gem "test/unit" broke
other gems (redgreen and activesupport)...

I rewrote my tests avoiding the SomeClass.stubs(:new) calls, and got all my
test passed...

Thanks anyway =)
Lucas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Silberbauer  
View profile  
 More options Mar 18 2010, 4:03 pm
From: James Silberbauer <james...@telkomsa.net>
Date: Thu, 18 Mar 2010 22:03:29 +0200
Local: Thurs, Mar 18 2010 4:03 pm
Subject: Re: Mocking one test is mocking every next test
I had this effect a while ago.
rake worked normally but autotest led to the behaviour you describe.

I changed config/environment.rb to contain:
  config.gem "thoughtbot-shoulda", :lib => false, :source =>
"http://gemcutter.org"
  config.gem "faker", :lib => false, :source => "http://gemcutter.org"
  config.gem "mocha", :lib => false, :source => "http://gemcutter.org"

- with all the :lib => false settings autotest works properly.

James


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »