Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression with to_return_json and procs #1034

Closed
jasonkarns opened this issue Aug 28, 2023 · 1 comment
Closed

Regression with to_return_json and procs #1034

jasonkarns opened this issue Aug 28, 2023 · 1 comment

Comments

@jasonkarns
Copy link

The change in this PR #1012 has changed the timing when a request stub's response proc is called.

Given:

specify do
  req_stub = stub_request(:post, url).to_return_json(body: proc { puts "REQUEST"; {}})
  puts "SUBJECT"
  subject # stand-in for expectations on the subject that would trigger the mocked request
end

In webmock 3.18.1, the output is "SUBJECT" then "REQUEST".
In webmock 3.19.0, the output is "REQUEST" then "SUBJECT".

It is expected that the proc for the request not actually fire until the mock is actually requested. This way the mock can perform side effects at the correct moment in the subject's lifecycle. In actuality, this bug was caught by an rspec change matcher that started failing after the webmock upgrade. The request stub's proc triggered side effects that replicated the real service, and so the change matcher was used to assert the subject under test was causing the changes. With the upgraded webmock, the expected side effect was occurring prior to the subject being called.

@bblimke
Copy link
Owner

bblimke commented Aug 29, 2023

@jasonkarns thank you for reporting.

This is not really a regression, since before PR #1012 , the support for declaring Proc as body in to_return_json was only accidental and not supported, but you have raised a valid point, that the body should be evaluated at the time of request and not at the time of to_return_json declaration, same as in case of to_return

I have released 3.19.1 with a fix.

@bblimke bblimke closed this as completed Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants