rspec/rspec-expectations — Issue #98: Pass a block to matcher
Author: wojciech
Created: 2011-12-03 13:42:08+00:00
State: open

Currently I'm doing something like:

expect { ... }.to send_an_email_to("example@example.com").
which(lambda{|email| email.subject =~ /expected/ })

Whereas I would like to do the following:

expect { ... }.to send_an_email_to("example@example.com").
which{|email| email.subject =~ /expected/ }

In other words I'd like to get rid of the lambda.

Following hack seems to do the trick:

RSpec::Matchers::Matcher.class_eval do
#override chain to pass blocks too
def chain(method, &block)
define_method method do |*args, &chain_block|
block.call(*args, &chain_block)
self
end
end
end

Whatcha think?

I haven't used Rspec2 since the fall of The Berlin Wall and I'm just getting my feet wet so it's totally possible I'm doing it very backwards (-: