I'm building an API POST call that includes data to be sent in the call. One piece of that data is a transaction_id, which I'm creating using the following code:
transaction_id = Digest::SHA1.hexdigest([Time.now, rand].join)[0..10].to_i(16).to_s(10)
I'm also using RSpec 2.12.2, which doesn't have allow
defined yet, so I can't do or even test something like
allow(Digest::SHA1).to receive(:hexdigest).and_return("transaction_id")
How can I mock a transaction_id in RSpec? I can freeze time, but having the rand in [Time.now, rand]
returns different results even if time is frozen.
I found that this works:
Digest::SHA1.any_instance.stubs(:hexdigest).returns("transaction_id")
based on examples here: https://github.com/freerange/mocha
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.