0

我正在为我的 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'})

       ============================================================

我搜索了很多并找到了一些解决方案,即

解决方案 1

津津乐道的文档

“WebMock::NetConnectNotAllowedError”</a>

我也看过这篇文章,但徒劳无功。

我也尝试使用WebMock.disable_net_connect!(:allow_localhost => true) 但得到了相同的结果。有人知道我在做什么错吗?我对 ruby​​ 真的很陌生,我第一次写规范,这让我很困惑。

4

1 回答 1

1

正如您提到的各种帖子所说,您不能使用 webmock 发出外部 http 请求。我注意到您的存根 url 在其中包含插值变量,即"https://test_sid:test_token@twilix.exotel.in/v1/Accounts/#{Generator::configuration.sid}/Sms/send". 你需要:

  1. 确保Generator::configuration.sid在规格范围内可访问
  2. 如果它无法访问,那么只需返回一个非常好的纯 url
于 2016-07-20T05:38:57.913 回答