RSpec 在比较多行字符串时提供“diff”样式的输出。在比较数组时有没有办法做类似的事情(除了将数组转换为多行字符串)?
最佳答案
我可能弄错了,但我不认为这个功能是 RSpec 内置的。
但是,您可以 implement a custom matcher with a custom error message :
RSpec::Matchers.define(:eq_array) do |expected|
match { |actual| expected == actual }
failure_message do |actual|
<<~MESSAGE
expected: #{expected}
got: #{actual}
diff: #{Diffy::Diff.new(expected.to_s, actual.to_s).to_s(:color)}
MESSAGE
end
end
# Usage:
expect([1, 2, 3]).to eq_array([1, 4, 3])
此演示使用 diffy
图书馆;您可以按照您认为合适的方式实现此操作。
关于arrays - 为 RSpec 数组匹配器启用差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46566468/