Today I Learned

Problems matching body params using stub_request?

Surprised that

HTTParty.post(
  'http://example.com/something',
  headers: { content_type: 'application/x-www-form-urlencoded' },
  body: { name: 'Tony', surname: 'Stark'}
)

is not matched by

stub_request(:post, 'http://example.com/something').
  with(
    headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
    body: { name: 'Tony', surname: 'Stark' }
  ).to_return(body: 'OK')

?

Remember, that HTTP Client does not send actually a hash, and stub_request does not know automagically which encoding you intended to use for your body params. You need to make it explicit, i.e.

stub_request(:post, 'http://example.com/something').
  with(
    headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
    body: URI.encode_www_form({ name: 'Tony', surname: 'Stark' })
  ).to_return(body: 'OK')