Nick
Nick

Reputation: 2911

Rails Rspec integer equals string ("1" == 1)

How, in rspec, do you compare the value of something while ignoring the type?

Failure/Error: expect(variable).to eql model.id

  expected: 1234
       got: "1234"

  (compared using eql?)

I've tried eq (which compares using ==) and eql (which compares using eql?)... I've also read https://stackoverflow.com/a/32926980/224707.

How do I make rspec consider those two values equal?

Upvotes: 2

Views: 3621

Answers (2)

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

== checks both instance type and value to be same so you need to convert them to be same

Change it to either of one

expect(variable.to_i).to eql model.id

or

expect(variable).to eql model.id.to_s

Upvotes: 3

Eric Duminil
Eric Duminil

Reputation: 54223

Instances of different classes cannot be equal.

You need to convert them so they become instances of the same Class :

"1234" == 1234
#=> false
"1234".to_i == 1234
#=> true
1234.to_s == "1234"
#=> true

So in your example :

expect(variable.to_i).to eql model.id
# or less logical :
expect(variable).to eql model.id.to_s

Upvotes: 9

Related Questions