0

私は宝石の仕様を書いており、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