[mocha-developer] Problem stubbing instances referred to by constants

James Mead jamesmead44 at gmail.com
Mon Sep 25 17:46:04 EDT 2006

On 25/09/06, Chris Roos <chrisjroos at gmail.com> wrote:
> Hi James,
> An instance retains its mocha between tests if that instance is
> pointed to by a constant.  The simplest failing test is something like
> (add as a new file in mocha/test)..
> #--code--
> require File.join(File.dirname(__FILE__), "test_helper")
> require 'stubba'
> class WemStubbaTest < Test::Unit::TestCase
>   Arr = [1, 2, 3]
>   def test_a
>     Arr.stubs(:size).returns(6)
>     assert_equal 6, Arr.size
>   end
>   def test_b
>     assert_equal 3, Arr.size #=> Failing test when both tests run
> consecutively
>   end
> end
> #--/code--
> I got as far as noticing that Stubba::InstanceMethod#unstub is
> 'intentionally empty'.  This is fine for most object instances but not
> ones that hang around (like the array above).  Sadly, I don't have (or
> even know whether there should be) a fix.

Hi Chris,

I'm pretty sure the solution is just to make InstanceMethod even more like
ClassMethod in #stub & #unstub. I think the difference was only ever an
optimization with the assumption that instances wouldn't persist between

However, I also wonder if this scenario is itself a code smell i.e. I'm not
sure anything should persist between tests - but maybe your actual scenario
involves a constant within an external class...?

I'll try to have a look at it soon.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060925/95a34140/attachment.html 

More information about the mocha-developer mailing list