We''re using Mocha stubs to stub out a slow-performing network request in a Test::Unit class. But naturally, we don''t want to stub it out in the few tests that actually verify the functionality of that network request. Is there a way to do the stub in "setup", but override it for the few tests that need the original functionality? (I realize that in this case, the best solution is to refactor the class under test, and pull the network functionality into its own class. But I can imagine there might be other scenarios where I''d want to do this too.) Jay Levit
Duncan Beevers
2007-Sep-06 19:36 UTC
[mocha-developer] Removing/overriding/unstubbing a stub?
A cheap way to do this would be to move the methods that actually test the network functionality to their own instance of Test::Unit::TestCase without the stubbing setup. On 9/6/07, Jay Levitt <lists-mocha at shopwatch.org> wrote:> We''re using Mocha stubs to stub out a slow-performing network request in > a Test::Unit class. But naturally, we don''t want to stub it out in the > few tests that actually verify the functionality of that network request. > > Is there a way to do the stub in "setup", but override it for the few > tests that need the original functionality? > > (I realize that in this case, the best solution is to refactor the class > under test, and pull the network functionality into its own class. But > I can imagine there might be other scenarios where I''d want to do this too.) > > > Jay Levit > > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >
Personally I''m not a big fan of the setup method. I agree with Duncan that two separate test cases is one way to go. Alternatively you could move the stubbing out of the setup method and into invidual test methods and only add it to the ones that need it - I think this has the benefit of being clearer and more explicit, but it will depend a bit on how many test methods need the stubbed method. There has been an item on the todo list for a long time about being able to "unstub" a method (I think it was first raised by Kevin Clark), but not many people have asked for it and we (at Reevoo) haven''t needed it. -- James. http://blog.floehopper.org http://tumble.floehopper.org
Duncan Beevers
2007-Sep-13 21:03 UTC
[mocha-developer] Removing/overriding/unstubbing a stub?
I think in most cases needing to unstub a method is an indication of over-reaching setup methods and too-aggressive initial stubbing. There are some instances however, where I''d like to unstub a method before teardown. For an example of such a case (good old Time.now) and a potential approach, see this thread http://rubyforge.org/pipermail/mocha-developer/2007-August/000409.html On 9/13/07, James Mead <jamesmead44 at gmail.com> wrote:> Personally I''m not a big fan of the setup method. I agree with Duncan > that two separate test cases is one way to go. Alternatively you could > move the stubbing out of the setup method and into invidual test > methods and only add it to the ones that need it - I think this has > the benefit of being clearer and more explicit, but it will depend a > bit on how many test methods need the stubbed method. > > There has been an item on the todo list for a long time about being > able to "unstub" a method (I think it was first raised by Kevin > Clark), but not many people have asked for it and we (at Reevoo) > haven''t needed it. > > -- > James. > http://blog.floehopper.org > http://tumble.floehopper.org > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >
On 9/13/2007 7:32 AM, James Mead wrote:> Personally I''m not a big fan of the setup method. I agree with Duncan > that two separate test cases is one way to go. Alternatively you could > move the stubbing out of the setup method and into invidual test > methods and only add it to the ones that need it - I think this has > the benefit of being clearer and more explicit, but it will depend a > bit on how many test methods need the stubbed method.Yeah, that''s how we initially coded it, but calling the same stub 25 times out of 28 tests felt pretty nasty! But for me, refactoring the test (and probably the functionality) into two classes solves the problem so far. I''ll yell again if there''s a case where it doesn''t or where I wish it weren''t needed. Jay
Cool. Thanks. On 14/09/2007, Jay Levitt <lists-mocha at shopwatch.org> wrote:> > On 9/13/2007 7:32 AM, James Mead wrote: > > Personally I''m not a big fan of the setup method. I agree with Duncan > > that two separate test cases is one way to go. Alternatively you could > > move the stubbing out of the setup method and into invidual test > > methods and only add it to the ones that need it - I think this has > > the benefit of being clearer and more explicit, but it will depend a > > bit on how many test methods need the stubbed method. > > Yeah, that''s how we initially coded it, but calling the same stub 25 > times out of 28 tests felt pretty nasty! > > But for me, refactoring the test (and probably the functionality) into > two classes solves the problem so far. I''ll yell again if there''s a > case where it doesn''t or where I wish it weren''t needed. > > Jay > > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >-- James. http://blog.floehopper.org http://tumble.floehopper.org