Stub and yield passed block
andyw8 opened this issue · comments
I was looking for way to stub a method and simply yield the block.
After trying some awkward approaches, I discovered this works with simply stubs(:foo).yields
, but this form without arguments doesn't seem be documented:
https://www.rubydoc.info/github/floehopper/mocha/Mocha%2FExpectation:yields
If this is a valid use then I can submit a PR to update the docs.
@andyw8 The *parameters
in the Mocha::Expectation#yields(*parameters) ⇒ Expectation
signature in the docs means that you can list zero, one, or more parameters. However, perhaps people aren't familiar enough with this notation. I'd be happy to include another example in the docs which has no parameters being yielded to the block if you think that would make things clearer.
Yeah, I understand that *parameters
can mean zero, but it felt a little like 'undocumented behaviour'. I've opened #496 to address.
For context here's where I'm using it:
@andyw8 The
*parameters
in theMocha::Expectation#yields(*parameters) ⇒ Expectation
signature in the docs means that you can list zero, one, or more parameters. However, perhaps people aren't familiar enough with this notation. I'd be happy to include another example in the docs which has no parameters being yielded to the block if you think that would make things clearer.
There already is such an example:
mocha/lib/mocha/expectation.rb
Line 274 in 0085b60
Perhaps it's not clear enough because of mixing yields and returns, and not being explicit about 'no/without parameters'.
Thanks, @nitishr. Yes, that was my assumption too. I'm always keen to improve the documentation and so if separating the examples makes things clearer then I'm in favour of it. In fact in this case, it might be clearer not to call Expectation#returns
in any of the examples for Expectation#yields
. What do you think?
Yeah, I think focusing on just the yields
without mixing with returns
would be clearer. I'm happy to update the example(s) if you'd like.
Yeah, I think focusing on just the
yields
without mixing withreturns
would be clearer. I'm happy to update the example(s) if you'd like.
Thanks. I've already merged something along those lines in d590812.