Request with WebMock and queries
Jun 14, 2021
If you would like to implement a request api stub, you have to consider WebMock first. It is very useful basically. At this time, I got confused about how to send a request with queries as a stub, so I’m going to take a note.
webmock
What if we want to send a request with queries?
For example, it is
https://example.com/pages/1?access_token="abcdefg"
like this.
Even if you write like below,
stub_request(:post, "https://example.com/pages/1?access_token="abcdefg").to_return(status: 200)
it won’t work.
We have to do like this.
stub_request(:post, "https://example.com/pages/1").with(query: hash_including({ 'access_token' => "abcdefg" })).to_return(status: 200)
It will work!