On Saturday, February 8, 2014 1:06:55 AM UTC-8, Jimmy wrote:
>
> Hello,
>
> I am wondering if there is a way to use a custom failure message for 
> message expectations with rspec-mocks. I'm writing some helper methods that 
> use message expectations under the hood. Unlike custom matchers from 
> rspec-expectations, message expectations always give a failure message like 
> this:
>
>     Failure/Error: Unable to find matching line from backtrace
>       Exactly one instance should have received the following message(s) 
> but didn't: foo
>
> I would like to be able to output an error message that speaks in the 
> domain of my program. As an example, I'd like to use this text:
>
>     Failure/Error: Message failed to trigger route
>       Expected message "bar" to route to :foo, but didn't.
>
> Is something like this possible? Thanks!
>
> Jimmy
>

There are two ways you could achieve this:

   -  Rather than using a normal message expectation, use a block 
   implementation that keeps track of whether or not the message expectation 
   has been satisifed, and raises an appropriate error if not.
   
called = false
allow(obj).to receive(:foo) { called = true }

# do stuff

expect(called).to eq(true), "Expected message "bar" to route to :foo, but 
didn't."

   - Write a matcher that works similarly to the `have_received` matcher.

allow(obj).to receive(:bar)

# do stuff

expect(obj).to have_routed(:bar).to(:foo)

Within your `have_routed` matcher, you can have it delegate to 
`have_received` internally, since you can use matchers from within custom 
matchers.

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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/c03bdb02-91cb-4dbe-8513-cd138ad794c0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to