在存根请求中遇到webmock问题

时间:2016-07-20 04:45:19

标签: ruby-on-rails ruby rspec exotel-api

我正在写我的宝石的规格,我正在使用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'})

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

我已经google了很多,并找到了一些解决方案,即

Solution 1

Relish Documentation

“WebMock::NetConnectNotAllowedError”

我也看了post,但是徒劳无功。

我也尝试使用WebMock.disable_net_connect!(:allow_localhost => true) 但得到了相同的结果。谁知道我做错了什么?我对ruby来说真的很新,而且我第一次编写规范并且让我很困惑。

1 个答案:

答案 0 :(得分:1)

您无法使用webmock发出外部http请求,正如您提到的各种帖子所说的那样。我注意到你的存根URL有内插变量,即"https://test_sid:test_token@twilix.exotel.in/v1/Accounts/#{Generator::configuration.sid}/Sms/send"。你需要:

  1. 确保在规范
  2. 中可以访问Generator::configuration.sid
  3. 如果它无法访问,那么只需返回一个非常好的简单网址