我试图更新这个测试,但它无法比较相同的字符串,即使我将“got”输出复制并粘贴回测试用例。为什么此 RSpec 测试失败?
Failure/Error: expect(first_item_cost).to eq("12 x $499 = $5,988")
expected: "12 x $499 = $5,988"
got: "12 x $499 = $5,988"
(compared using ==)
代码:
first_item_cost = find('.cart-item-cost', match: :first).text
expect(first_item_cost).to eq("12 x $499 = $5,988")
RSpec 3.9
最佳答案
我检查了编码和字节,发现:
puts "Encoding: " + first_item_cost.encoding.to_s
puts "Bytes: " + first_item_cost.bytes.to_s
输出:
Encoding: UTF-8
Bytes: [49, 50, 32, 195, 151, 32, 36, 52, 57, 57, 32, 61, 32, 36, 53, 44, 57, 56, 56]
“x”的字节数太多!我查看了模板,果然它使用了 ×
。当我从控制台复制和粘贴时,它一定丢失了原始字符(或者 RSpec 在输出之前翻译了它)。我将规范和模板更改为 x
。
关于ruby-on-rails - RSpec expect 在比较相等的字符串时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69887434/