[mocha-developer] problem with using any_instance

Yi Wen hayafirst at gmail.com
Fri Mar 30 13:33:15 EDT 2007


sorry I mis-presented my case.

1. method_name is an instance method of klass
2. I do NOT want to mock  this method in test_b

So I guess there is something missing in unstub chain upon exiting test_a or
I need to do some teardown myself?

Thanks

Yi

On 3/30/07, Matt Chen <chen.mingquan at gmail.com>

> 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
> >
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
>


More information about the mocha-developer mailing list