[rspec-users] Question: Define custom matcher like raise_error, by Matchers.define

David Chelimsky dchelimsky at gmail.com
Thu Nov 25 07:04:27 EST 2010


On Nov 24, 2010, at 11:09 PM, 白井 薫 wrote:

> I have a question about the custom matcher using Matcher#define.
> 
> My intention is given by the following code:
> 
> ========================================================================
> Spec::Matchers.define :be_done do
>  match do |block|
>    block.call
>    true
>  end

This should work:

match do |block|
  begin
    block.call
    true
  rescue
    false
  end
end

Or, if you prefer one liners:

match do |block|
  (block.call; true) rescue false
end

HTH,
David

> 
>  failure_message_for_should do
>    "be_done failed."
>  end
> end
> 
> describe "The expectation failure in block called from the custom matcher" do
>  example "should be reported." do
>    lambda {
>      lambda {
>        1.should == 2
>      }.should be_done
>    }.should_not raise_error(/be_done failed/)   # should 'expected: 2, got: 1 (using ==)'
>  end
> end
> ========================================================================
> 
> I want to define the custom matcher that it calls the block given as the actual value, like raise_error.
> But if the expectation in the given block failed,
> it is not reported, but it reports the custom matcher's failure.
> 
> Is there some misuse about Matchers.define in my code?
> 
> Best Regards.
> --
> Kaoru Kobo
> 
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list