我正在开发 Ruby on Rails gem,我正在尝试 webmock,因为我需要交互(和测试)不受我控制的外部 API。
所以,这是before(:each)
因为我在那里存根的片段:
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
在这里你可以看到rspec
命令的控制台输出:
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
如果我同时删除:query
onHTTParty.get
和 in的键stub_request
,它可以工作,但我需要这些查询键和值来测试 API。
我什至通过替换 in 进行了测试,stub_request
但有同样的问题。uri
/searchContact.json/
在这里您可以找到GIST