Chris Roos
2006-Sep-25 17:43 UTC
[mocha-developer] Problem stubbing instances referred to by constants
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. Chris
James Mead
2006-Sep-25 21:46 UTC
[mocha-developer] Problem stubbing instances referred to by constants
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 tests. 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. Thanks. -- James. http://blog.floehopper.org -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060925/95a34140/attachment.html
Chris Roos
2006-Sep-26 07:36 UTC
[mocha-developer] Problem stubbing instances referred to by constants
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 > tests. >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. Chris
James Mead
2006-Sep-26 09:19 UTC
[mocha-developer] Problem stubbing instances referred to by constants
On 26/09/06, Chris Roos <chrisjroos at gmail.com> wrote:> > 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. >Thanks for the patch - I''ve committed it. -- James. http://blog.floehopper.org -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060926/e79ce956/attachment.html