ruby - How to append custom message to RSpec exception message -


is there way prefix/append custom message rspec exception?

expect(products).to match_array expected_products 

i aware can pass exception message param above expectation don't want have own exception add rspec exception i'd retain. above statement raise expected [...] match [...] missing...extra... find useful have e.g. customer id added message e.g. customer id: 12345 - expected [...] match [...] missing...extra... perhaps monkey patching?

you can monkey patch handle_failure method.

module myexpectationhelper   def self.handle_failure(matcher, message, failure_message_method)     message = message.call if message.respond_to?(:call)     message ||=''     message += matcher.__send__(failure_message_method) #changes ||= += prepend message     if matcher.respond_to?(:diffable?) && matcher.diffable?       ::rspec::expectations.fail_with message, matcher.expected, matcher.actual     else       ::rspec::expectations.fail_with message     end   end end  describe 'append message'   module rspec     module expectations       module expectationhelper         def self.handle_failure(matcher, message, failure_message_method)           myexpectationhelper.handle_failure(matcher, message, failure_message_method)         end       end     end   end   'should contain message'     expect(products).to match_array expected_products, 'customer id: 12345 -  '   end end 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -