Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec expect is failing while comparing equal strings

I tried to update this test but it is failing to compare identical strings, even though I copied and pasted the "got" output back into the test case. Why is this RSpec test failing?

 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 ==)

Code:

first_item_cost = find('.cart-item-cost', match: :first).text
expect(first_item_cost).to eq("12 x $499 = $5,988")

RSpec 3.9

like image 319
Chloe Avatar asked Jan 27 '26 20:01

Chloe


1 Answers

I checked the encoding and bytes and discovered:

puts "Encoding: " + first_item_cost.encoding.to_s
puts "Bytes: " + first_item_cost.bytes.to_s

Output:

Encoding: UTF-8
Bytes: [49, 50, 32, 195, 151, 32, 36, 52, 57, 57, 32, 61, 32, 36, 53, 44, 57, 56, 56]

The 'x' has too many bytes! I looked in the template and sure enough it used ×. When I copied and pasted from the console, it must have lost the original character (or RSpec translated it before output). I changed the spec and template to x.

like image 110
Chloe Avatar answered Jan 29 '26 11:01

Chloe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!