Created
March 17, 2012 21:30
-
-
Save myronmarston/2065445 to your computer and use it in GitHub Desktop.
Proof of concept for yield_value rspec matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
I'll start taking a stab at this, and open a pull request with what I come up with.