[mocha-developer] Problem stubbing instances referred to by constants
chrisjroos at gmail.com
Tue Sep 26 03:36:56 EDT 2006
On 9/25/06, James Mead <jamesmead44 at gmail.com> wrote:
> 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)..
> > <snipped 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
You're right, of course. As a quick check, I removed the over-ridden
methods (InstanceMethod#stub and InstanceMethod#unstub) and the
failing test provided originally passes.
> 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...?
Hmm, not too sure really. We have a constant that gets set to an
instance of two possible classes at rails start time (e.g. Foo =
Bar.new OR Foo = Baz.new). I guess we're providing singleton'ish
access to the instantiated object.
More information about the mocha-developer