我正在为我的 gem 编写规范,我正在使用 webmock 来模拟 http 请求。但我不断收到这个奇怪的错误。
这是我的规格代码
require 'spec_helper'
describe 'Generator::Exotel' do
describe '#success' do
let(:resps) { {"Status"=>"200", "Message"=>"Success"} }
before do
stub_request(:post, "https://test_sid:test_token@twilix.exotel.in/v1/Accounts/#{Generator::configuration.sid}/Sms/send").
with(:body => {:To => 1234, :Body => "test sms"}, :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:body => resps.to_json, :headers => {})
end
it 'returns response object for success' do
response = Generator::Exotel.send(:to => 1234, :body => "test sms")
expect(response.to_json).to eq (resps.to_json)
end
end
describe '#failure' do
let(:resp) { {"Status"=>"401", "Message"=>"Not Authenticated"} }
before do
stub_request(:post, "https://test_sid:test_token@twilix.exotel.in/v1/Accounts/#{Generator::configuration.sid}/Sms/send").
with(:body => {:To => 1234, :Body => "test sms"}, :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:body=> resp.to_json, :headers => {})
end
it 'returns response object for failure' do
response = Generator::Exotel.send(:to => 1234, :body => "test sms")
expect(response.to_json).to eq (resp.to_json)
end
end
end
每当我运行rspec
时,我都会收到以下错误
Generator::Exotel #success returns response object for success
Failure/Error: response = self.class.post("/#{Generator::configuration.sid}/Sms/send", {:body => params, :basic_auth => auth })
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: POST https://twilix.exotel.in/v1/Accounts/test_sid/Sms/send with body 'To=1234&Body=test%20sms' with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Basic dGVzdF9zaWQ6dGVzdF90b2tlbg==', 'User-Agent'=>'Ruby'}
You can stub this request with the following snippet:
stub_request(:post, "https://twilix.exotel.in/v1/Accounts/test_sid/Sms/send").
with(:body => "To=1234&Body=test%20sms",
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Basic dGVzdF9zaWQ6dGVzdF90b2tlbg==', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})
registered request stubs:
stub_request(:post, "https://test_sid:test_token@twilix.exotel.in/v1/Accounts/test_sid/Sms/send").
with(:body => {"Body"=>"test sms", "To"=>1234},
:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})
============================================================
我搜索了很多并找到了一些解决方案,即
“WebMock::NetConnectNotAllowedError”</a>
我也尝试使用WebMock.disable_net_connect!(:allow_localhost => true)
但得到了相同的结果。有人知道我在做什么错吗?我对 ruby 真的很陌生,我第一次写规范,这让我很困惑。