Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec mock should_receive in custom matchers

I am trying to use RSpec mock and should_receive in custom matcher. I want to catch the error caused by the should_receive to return proper value from the matcher and cause it output my custom failure message.

How to do it? Or maybe I should change my approach?

like image 303
wrzasa Avatar asked Nov 12 '12 20:11

wrzasa


1 Answers

The answer is:

match do |obj|
  # do some setup and mocks here   
  begin
    RSpec::Mocks::verify  # run mock verifications
    true
  rescue RSpec::Mocks::MockExpectationError => e
    # here one can use #{e} to construct an error message
    false
  end
end

Finally found it here

like image 121
wrzasa Avatar answered Oct 13 '22 23:10

wrzasa