I am working on a Ruby on Rails stone and I am trying to use webmock because I need to interact (and test) an external API that is not under my control.
So here is the snippet that is in before(:each), because I bit it:
before do
uri = URI.join(client.class.base_uri, DataComApi::ApiURI.search_contact).to_s
stub_request(
:get,
uri
).with(
query: hash_including({
'pageSize' => 0,
'offset' => 0
})
).to_return(
body: FactoryGirl.build(
:data_com_search_contact_response,
totalHits: 0
).to_json
)
require 'httparty'
HTTParty.get(
uri,
{
query: {
offset: 0,
pageSize: 0
}
}
)
end
And here you can see the console output of the command rspec:
3) DataComApi::Client
Failure/Error: HTTParty.get(
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: GET https://www.jigsaw.com/rest/searchContact.json?offset=0&pageSize=0
You can stub this request with the following snippet:
stub_request(:get, "https://www.jigsaw.com/rest/searchContact.json?offset=0&pageSize=0").
to_return(:status => 200, :body => "", :headers => {})
registered request stubs:
stub_request(:get, "https://www.jigsaw.com/rest/searchContact.json with query params hash_including({"offset"=>0, "pageSize"=>0})")
============================================================
If I delete the key :queryboth on HTTParty.getand in stub_request, it works, but I need these keys and values ββto check the API.
I tested even replacing in stub_request, urion /searchContact.json/, but had the same problem.
Here you can find GIST
source
share