[mocha-developer] problem with using any_instance

Matt Chen chen.mingquan at gmail.com
Fri Mar 30 12:57:47 EDT 2007


Every test case would be run separately in unit test system, which means
test_a does not have any relationship with test_b and of coz this statement
in test_a

klass.any_instance.stubs(:method_name).returns("something")

will not influence any code in method test_b.

I think if you move that statement above to the method setup it will works
for you, since this method would be invoked before each test case starting.


On 3/31/07, Yi Wen <hayafirst at gmail.com> wrote:
>
> Hey all,
>
> I have a question with using mocha in my tests.
>
> In the same test file, I have two tests,
>
> <code>
> def test_a
>   klass.any_instance.stubs(:method_name).returns("something")
>   klass.new.method_name
>   ...
> end
>
> def test_b
> ...
>    klass.new.method_name
> ...
> end
> </code>
>
> where klass is some class
>
> when the tests run,  test _a passes, but test_b had an error like this:
>
> test_b
> NoMethodError: undefined method `method_name' for #<klass:0xb6fb1934>
>
> I put a
> <code>
>   puts 'unstub called'
> </code>
> in unstub of any_instance_method.rb and it got called wen running test_a
>
> Any idea what the problem it could be?
>
> Thanks,
>
> Yi
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
>


More information about the mocha-developer mailing list