我写了一个名为have_backtrace / error_with_backtrace的可组合匹配器,如下所示:
RSpec::Matchers.define :have_backtrace do |expected_backtrace|
match do |error|
error.backtrace == expected_backtrace
end
failure_message do |error|
"expected error to have backtrace #{expected_backtrace.inspect}, actually got #{error.backtrace.inspect}"
end
end
RSpec::Matchers.alias_matcher :error_with_backtrace, :have_backtrace
然后我可以像这样使用它:
callback = double('reject_callback').as_null_object
# Do some stuff which could call callback.call
expect(callback).to have_received(:call).with(error_with_backtrace(caller))
这很好用,但错误信息并非我想要的
expected: (error with backtrace "backtrace_line_1", "backtrace_line_2", ... )
got: (#<RuntimeError: RuntimeError>)
我希望&#39;得到&#39;部分还要说(error with backtrace ...)
有没有办法实现这个目标?