失败时的漂亮打印数组 [英] Pretty-print arrays on failure
问题描述
describe Rspec do
it 'should print arrays in a readable manner' do
arr = [
[0, :a, -1],
[1, :b, -2],
[2, :c, -3],
[3, :d, -4],
[4, :e, -5],
[6, :g, -7],
[7, :h, -8],
[8, :i, -9]
]
arr.should eql []
end
end
失败时:
Failures:
1) Rspec should print arrays in a readable manner
Failure/Error: arr.should eql []
expected: []
got: [[0, :a, -1], [1, :b, -2], [2, :c, -3], [3, :d, -4], [4, :e, -5], [6, :g, -7], [7, :h, -8], [8, :i, -9]]
有没有办法告诉 Rspec 漂亮地打印它的失败?我的实际示例可以在数组中包含 10 到 40 个元素,每个元素是一个包含 5 个整数和一个字符串的数组.
Is there a way to tell Rspec to pretty print its failures? My real-world example can have anywhere from 10 - 40 elements in the array, with each element being an array of 5 ints and a string.
推荐答案
虽然这不是处理所有失败消息中所有对象的显示的通用解决方案,但您可以使用所描述的技术为任何一个示例自定义失败消息在 https://www.relishapp.com/rspec/rspec-expectations/文档/自定义消息.
Although this is not a generic solution for handling display of all objects in all failure messages, you can customize a failure message for any one example, using the technique described in https://www.relishapp.com/rspec/rspec-expectations/docs/customized-message.
结合 Ruby 的标准漂亮打印函数的定制,使用更小的线宽并将其结果作为字符串返回,为您提供:
Combined with a customization of Ruby's standard prettyprint function to use a smaller line width and return its result as a string, gives you:
arr.should be_empty, "expected: empty array\ngot:\n#{PP.pp(arr,'',20)}"
这篇关于失败时的漂亮打印数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!