rspec-expectations: It would be nice to add `.to receive(...).with_block`
There is no easy way to verify a method would be called with a block.
It would be nice to have
expect(thing).to receive(:method).with_block
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 1
- Comments: 15 (7 by maintainers)
@benoittgt I think @ioquatix is talking about
receive(...)from rspec-mocks, so that you can expect to receive a message with a block. You can currently deal with this by passing a block toreceive:If we wanted to add more explicit support for this, I’d rather not add a new
with_blockAPI; instead maybe something like:Where
a_blockis a “special” argument matcher likeany_argsorno_argsthat doesn’t match on a positional arg but instead rspec-mocks handles internally.@ioquatix does the approach I suggested above work for you?
I’ve expanded on @kaiwren’s work over on rspec/rspec-mocks#1237, it’s worth noting we already had the
and_yieldexpectation, and I think there are actually a few issues witha_block, what happens when used positionally? and does it always mean the supplied&blockargument? After allobject.msg(proc {}, proc {})is valid Ruby, should we expect on this with.with(a_block, a_block)and how do we differentiated that frommsg(proc{}) { }.Hopefully, yes, we could make that work.
Do you want to take a stab at adding it. Improving RSpec is largely a self-service operation unless one of the core team members wants to take this work on (which often doesn’t happen; we all have lives and jobs!).
Thanks, @myronmarston for clarification. I agree with your proposal.
Hello @ioquatix
This is probably not what you are asking but have you seen? https://github.com/rspec/rspec-expectations/blob/v3.7.0/features/custom_matchers/define_block_matcher.feature
Also, it’s funny because factory_bot have a matcher for block with the name you mentioned. https://github.com/thoughtbot/factory_bot/blob/40c8708c473279d3f812a825ff979f2c93451e05/spec/support/matchers/trait.rb
Used like this: https://github.com/thoughtbot/factory_bot/blob/ad107bea15aa75b60decbbebbc86980fd3e85d34/spec/factory_bot/definition_proxy_spec.rb#L127-L130
Bye