On Sunday, February 1, 2015 at 2:17:43 AM UTC-8, Kerry Buckley wrote:
>
> I've recently discovered compound expectations in RSpec 3, which I really 
> like, but I noticed a difference in output if you use them within a custom 
> matcher. For example:
>
> RSpec::Matchers.define :be_acceptable do
>   match do |actual|
>     expect(actual).to be_even.and be > 10
>   end
> end
>
> describe "Composing matchers" do
>   it "works in an expectation" do
>     expect(1).to be_even.and be > 10
>   end
>
>   it "works inside another matcher" do
>     expect(1).to be_acceptable
>   end
> end
>
> Both expectations work, but the second loses the detailed failure 
> information:
>
> Failure/Error: expect(1).to be_even.and be > 10 expected `1.even?` to 
> return true, got false ...and: expected: > 10 got:   1 
> Failure/Error: expect(1).to be_acceptable expected 1 to be acceptable
>
> Is there a way to encapsulate a set of other matchers inside another 
> matcher, while retaining the full failure messages?
>
> Thanks,
>
> Kerry
>

By defining a custom be_acceptable matcher, you are telling RSpec you want 
the description and failure message to use “be acceptable” in the 
description and failure message.

OTOH, if you simply want to use be_acceptable, and don’t care about it 
actually being a custom matcher, you can define it simply as a well-named 
helper method that returns the compound matcher expression:

module MatcherHelpers
  def be_acceptable
     be_even.and be > 10
  endend
RSpec.configure do |c|
  c.include MatcherHelpersend

…which will give you the exact same failure as your first example.
HTH,
Myron

 

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rspec+unsubscr...@googlegroups.com.
To post to this group, send email to rspec@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/6d589813-f974-45c6-8f39-1a401263b68c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to