我试图将WebMock.disable_net_connect!
放在很多地方,但正如我在上一次测试中所建议的那样,我将它放在spec_helper.rb
中,如下所示:
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
我的规格如下:
describe Client do
describe '#get_something' do
context 'when post params are valid' do
it 'returns a response' do
request = stub_request(:post, 'https://myurl.com')
.to_return(my_response)
result = client.get_something(params)
expect(WebMock).to have_requested(:post, 'https://myurl.com')
end
end
end
end
嗯,当然是我的客户打电话:
class Client
def get_something(params)
RestClient.post @url, params
end
end
此消息失败:
Failure/Error: end
The request GET https://myurl.com/ was expected to execute 1 time but it executed 0 times
The following requests were made:
No requests were made.
============================================================
我使用的是rails 4.1.8,rspec 3.1.7,rspec-rails 3.1.0和webmock 1.8.11。现在,我正在创建一个模拟http_client来使事情正常工作,但是,这里的任何帮助都将受到赞赏!如果您需要更多信息,请告诉我们!
答案 0 :(得分:0)
您应该使用此代码:
WebMock.allow_net_connect!
或者,您也可以
WebMock.disable_net_connect(allow: ['https://myurl.com'])
如WebMock的文档中所示:
WebMock.allow_net_connect!
stub_request(:any, "www.example.com").to_return(:body => "abc")
Net::HTTP.get('www.example.com', '/') # ===> "abc"