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
Post a Comment