白井 薫
2010-Nov-25 05:09 UTC
[rspec-users] Question: Define custom matcher like raise_error, by Matchers.define
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 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
David Chelimsky
2010-Nov-25 12:04 UTC
[rspec-users] Question: Define custom matcher like raise_error, by Matchers.define
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 > endThis 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
Kaoru Kobo
2010-Nov-26 00:30 UTC
[rspec-users] Question: Define custom matcher like raise_error, by Matchers.define
Thanks, David.> > This should work: > ... > match do |block| > (block.call; true) rescue false > endI tried that, but the spec still fails... 1) ''The expectation failure in block called from the custom matcher should be reported.'' FAILED expected no Exception with message matching /be_done failed/, got #<Spec::Expectations::ExpectationNotMetError: be_done failed.> ./spec/custom_matcher_spec.rb:21: I expect that it fails with message ''expected: 2, got: 1 (using ==)''. Best Regards. -- Kaoru Kobo
Kaoru Kobo
2010-Nov-26 01:16 UTC
[rspec-users] Question: Define custom matcher like raise_error, by Matchers.define
Then I found this page: ?Upgrade to 1.3.0 - Matcher DSL" Upgrade.rdoc at master from dchelimsky''s rspec - GitHub http://github.com/dchelimsky/rspec/blob/master/Upgrade.rdoc and, I found the tentative solution: (Is there the better way?) ======================================================================= Spec::Matchers.define :be_done do - match do |block| + # Do not turn any exceptions to false. + match_unless_raises(Class.new(Exception)) do |block| block.call true end ======================================================================= Best Regards. -- Kaoru Kobo
David Chelimsky
2010-Nov-28 21:23 UTC
[rspec-users] Question: Define custom matcher like raise_error, by Matchers.define
On Nov 25, 2010, at 7:16 PM, Kaoru Kobo wrote:> Then I found this page: > > ?Upgrade to 1.3.0 - Matcher DSL" > Upgrade.rdoc at master from dchelimsky''s rspec - GitHub > http://github.com/dchelimsky/rspec/blob/master/Upgrade.rdoc > > > and, I found the tentative solution: > (Is there the better way?)Actually, match_unless_raises is probably the best solution for your issue.> > =======================================================================> Spec::Matchers.define :be_done do > - match do |block| > + # Do not turn any exceptions to false. > + match_unless_raises(Class.new(Exception)) do |block| > block.call > true > end > =======================================================================> > Best Regards. > -- > Kaoru Kobo
Kaoru Kobo
2010-Nov-29 04:32 UTC
[rspec-users] Question: Define custom matcher like raise_error, by Matchers.define
David Thanks!> > Actually, match_unless_raises is probably the best solution for your > issue. >> >> Then I found this page: >> >> ?Upgrade to 1.3.0 - Matcher DSL" >> Upgrade.rdoc at master from dchelimsky''s rspec - GitHub >> http://github.com/dchelimsky/rspec/blob/master/Upgrade.rdoc >> >> and, I found the tentative solution: >> (Is there the better way?)