Huy
Huy

Reputation: 11206

Stubbing string hash for value instead of using dot operator in rails tests

I have a unit test where I was stubbing place.ext_fb_place_id using

let(:place) { stub(:place, ext_fb_place_id: SecureRandom.random_number(20_000_000), facebook_metadata: {category: nil}, lat: 33.129147303422, lng: -96.653188420995, name: "In & Out Burger") }

I had to change my code to use the string key instead of the dot operator. That is, I had to use place["ext_fb_place_id'] to get the correct value. However, this throws the following error:

Stub :place received unexpected message :[] with ("ext_fb_place_id")

How do I stub the [] method so that I can use calls like place["ext_fb_place_id"] or place["lat"]?

Thanks

Upvotes: 0

Views: 194

Answers (1)

Huy
Huy

Reputation: 11206

I was able to stub with the following:

ext_fb_place_id = SecureRandom.random_number(20_000_000)

let(:place) { stub(:place, ext_fb_place_id: ext_fb_place_id, facebook_metadata: {category: nil}, lat: 33.129147303422, lng: -96.653188420995, name: "In & Out Burger", :[] => ext_fb_place_id)}

Thanks to @LeeJarvis and @JimDeville.

Upvotes: 0

Related Questions