I'm working on a Ruby on Rails gem and I'm trying webmock because I need to interact (and test) an external API not under my control.
So, here is the snippet which is in before(:each)
because I was stubbing it there:
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
)
# DEBUG
require 'httparty'
HTTParty.get(
uri,
{
query: {
offset: 0,
pageSize: 0
}
}
)
end
And here you can see the console output of rspec
command:
3) DataComApi::Client#search_contact returns instance of SearchContact
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})")
============================================================
# ./spec/models/client_spec.rb:65:in `block (3 levels) in <top (r
If I remove the :query
key both on HTTParty.get
and in stub_request
, it works, but I need those query keys and values to test the API.
I tested even by replacing in stub_request
, uri
with /searchContact.json/
but had same issue.
Here you can find a GIST
filter[xyz]=1
, you should mock withwith(query: hash_including(filter: { xyz: 1 }))
– Matherne