I've tried using a lambda in my custom response:
stub_request(
:post,
'http://blah.blah/token'
).to_return(
status: 200,
body: lambda { |a| '{"token":"' + SecureRandom.hex(20) + '","expires_in":"259200"}' }
)
Maybe this isn't the correct way to handle dynamic responses, but anyway, webmock seems to execute the lambda exactly once. The request is identical each time, so either:
- My asumption that using a lambda would allow me to generate dynamic content on a per-response basis was wrong.
- Because the repeated requests are identical, webmock just uses the last response it generated.
Since this question was written, I strongly suspect that something in Webmock has changed, because the following test passes:
Tested with Ruby 2.1.5p273, RSpec 3.3.1, and WebMock 1.21.0.