James Mead
2007-Apr-11  14:37 UTC
[mocha-developer] Fwd: [ mocha-Bugs-8687 ] Block''s return value is dropped on stubbed yielding methods.
---------- Forwarded message ----------
From: noreply at rubyforge.org <noreply at rubyforge.org>
Date: 11-Apr-2007 15:31
Subject: [ mocha-Bugs-8687 ] Block''s return value is dropped on stubbed
yielding methods.
To: noreply at rubyforge.org
Bugs item #8687, was opened at 2007-02-15 17:29
You can respond by visiting:
http://rubyforge.org/tracker/?func=detail&atid=7477&aid=8687&group_id=1917
Category: None
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Erik Hetzner (egh)
Assigned to: Nobody (None)
Summary: Block''s return value is dropped on stubbed yielding methods.
Initial Comment:
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
----------------------------------------------------------------------
>Comment By: Erik Hetzner (egh)
Date: 2007-04-11 07:31
Message:
Hi James,
Sorry I wasn''t more clear. I could certainly change my code,
but I have existing code that I want to test, & I don''t want
to have to change my code so that I can test it. My code
uses this with_something idiom & returns the value which the
yielded block returns. I can''t test this with Mocha''s yields
builder because it does not return the value with is
returned by the yield block.
The code:
baz = foo.with_something do |bar|
  bar.get_baz.to_s
end
is real code which I want to test with a mocked foo & bar
objects.
----------------------------------------------------------------------
Comment By: James Mead (jamesmead)
Date: 2007-04-11 07:03
Message:
Hi Erik,
Thanks for the bug report & suggested patch. To be honest I can''t
see
anything wrong with Mocha''s behaviour in the example you''ve
given. It''s not
obvious what you are trying to test. Can you not do something like this...
  def test_me
    foo = mock()
    bar = mock()
    bar.stubs(:get_baz).returns(:baz)
    foo.stubs(:with_something).yields(bar)
    baz = nil
    foo.with_something do |bar|
      baz = bar.get_baz.to_s
    end
    assert_equal ''baz'', baz
  end
(see http://pastie.caboo.se/53041 for syntax highlighted version)
...so I don''t really see the need for the yields_and_returns method you
are
suggesting. Does that make sense?
If not, can you try to explain more fully what exactly it is that you are
trying to test, ideally with full code examples.
Thanks, James.
----------------------------------------------------------------------
You can respond by visiting:
http://rubyforge.org/tracker/?func=detail&atid=7477&aid=8687&group_id=1917
-- 
James.
http://blog.floehopper.org
Apparently Analagous Threads
- [#8687] Block''s return value is dropped on stubbed yielding methods.
- Stubbing yielding methods
- Fwd: [ mocha-Bugs-16523 ] Ruby 1.9 gives warning
- Fwd: [ mocha-Bugs-12001 ] Method call count is not reported correctly on error
- Fwd: [ mocha-Bugs-5892 ] Using a setup method in test_case_class destroys subsequent test cases
