-2

When I am trying to mock a http request with the following code snippet.

mocked_payload = { test: 'testing' }.to_json

stub_request(:get, application_url)
  .with(body: platform.user_credentials)
  .to_return(status: 200, body: mocked_payload)

I receive this.

enter image description here

Does anyone know how this can be solved? I reduced the mocked_payload to figure out if something is wrong with that.

DenicioCode
  • 8,668
  • 4
  • 18
  • 33

2 Answers2

0

This error is happening because of .with(body: nil), when webmock tries to parse body json, but body is not defined for GET-requests

Vasfed
  • 18,013
  • 10
  • 47
  • 53
  • (byebug) platform.user_credentials {:username=>"this-is-a-partner-id", :password=>"this-is-an-api-key"} Inside the user_credentials is no nil. I use the same user_credetials for a stub_request in the same before block. – DenicioCode Mar 05 '20 at 09:46
  • It was the whole `.with(body: {})`. Removing it solves the issue, I added an answer. Thank you for your answer. – DenicioCode Mar 05 '20 at 10:38
0

After I dug more into the request I figured out how to solve it:

The http get request was stubbed with a body, which is never used in the "real" request.

Removing this line .with(body: platform.user_credentials) solved the issue.

DenicioCode
  • 8,668
  • 4
  • 18
  • 33