James Mead
2007-Feb-16 22:43 UTC
[mocha-developer] [#8687] Block''s return value is dropped on stubbed yielding methods.
Erik Hetzner has submitted a bug with a suggested patch...> Best explained with an example: > > test: > > foo = mock() > bar = mock().stubs(:get_baz).returns(:baz) > foo.stubs(:with_something).yields(bar) > > real code: > > baz = foo.with_something do |bar| > bar.get_baz.to_s > end > > baz should now be "baz", but is nil because we have not specified a return value for the stub. Now, we could > set add .returns("baz"), but that means that the block isn''t really tested. > > I have attached a file patches on a yields_and_returns method to Mocha::Expecatation & Mocha::Stub which will return > the block''s return value. > > foo = Mocha::Mock.new > bar = Mocha::Mock.new > bar.stubs(:get_baz).returns(:baz) > foo.stubs(:with_something).yields_and_returns(bar) > > baz = foo.with_something do |bar| > bar.get_baz.to_s > end > >My take on this is that if the #with_something method is stubbed, the only behaviour you should be able to specify for the stubbed method is what values it yields to a supplied block and what value it returns. I think what Erik is trying to do is trying to link the result of the block to the return value of the method. I think he would be better off with two tests - one testing the result of calling the the block in isolation and one testing the behaviour of #with_something passing in a simple block. I''m a bit tired at the moment, so it''s quite possible I''ve misread his code, but I thought I''d get this out onto the list to see if anyone had any thoughts. I''ll try and put some illustrative examples together over the weekend. -- James. http://blog.floehopper.org