Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created March 17, 2012 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/2065445 to your computer and use it in GitHub Desktop.
Save myronmarston/2065445 to your computer and use it in GitHub Desktop.
Proof of concept for yield_value rspec matcher
RSpec::Matchers.define :yield_value do |expected_yielded_value|
match do |block|
yielded_value = nil
block.call(lambda { |arg| yielded_value = arg })
yielded_value == expected_yielded_value
end
end
describe "yield_value matcher" do
it 'passes a valid positive expectation' do
expect { |b| 3.tap(&b) }.to yield_value(3)
end
it 'fails an invalid positive expectation' do
expect {
expect { |b| 3.tap(&b) }.to yield_value(2)
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
end
@myronmarston
Copy link
Author

BTW, on the topic of the naming of the matchers, I think I like yield_with_args better than yield_args. It has a nice symmetry to yield_without_args, and it's more closely aligned with how I talk about this stuff: "3.tap yields with 3 as the argument".

@myronmarston
Copy link
Author

I'll start taking a stab at this, and open a pull request with what I come up with.

@myronmarston
Copy link
Author

FWIW, I've pushed what I've got so far into a branch:

https://github.com/rspec/rspec-expectations/tree/yield_matchers

There's more I plan to do before submitting a pull request, but I figured I'd push something to back it up and so people who care to can take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment