2016-07-20 16 views
0

私は私の宝石のために筆記しています。私はHTTPリクエストを模擬するためにwebmockを使用しています。しかし、私はこの奇妙なエラーを取得し続けています。私はrspecを実行するたびにここでスタブのリクエストでwebmockに問題が発生しました

は私のスペックコード

require 'spec_helper' 

describe 'Generator::Exotel' do 

    describe '#success' do 
    let(:resps) { {"Status"=>"200", "Message"=>"Success"} } 

    before do 
     stub_request(:post, "https://test_sid:[email protected]/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:[email protected]/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 

で、私は、すなわち、

を多くのことをGoogleで検索し、いくつかの解決策を発見した、この次のエラー

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:[email protected]/v1/Accounts/test_sid/Sms/send"). 
     with(:body => {"Body"=>"test sms", "To"=>1234}, 
       :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}) 

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

を取得していますSolution 1

Relish Documentation

“WebMock::NetConnectNotAllowedError”

また、私は、このpostを見ていた、しかし、無駄インチ

また、私はWebMock.disable_net_connect!(:allow_localhost => true) を使ってみましたが、同じ結果が得られました。誰でも何が間違っているのか知っていますか?私は本当にルビーの新人ですし、初めて私は仕様書を書いていて、本当に混乱しています。

答えて

1

webmockで外部httpリクエストを行うことはできません。あなたが言及したさまざまな投稿の通りです。あなたのスタブされたURLには、変数"https://test_sid:[email protected]/v1/Accounts/#{Generator::configuration.sid}/Sms/send"が挿入されています。以下を行う必要があります。

  1. Generator::configuration.sidは、それがアクセス可能ではありません場合は、単に完全に罰金
+0

はい 'ジェネレータであるプレーンなURLを返す:: configuration.sid'があるスペック内

  • アクセス可能であることを確認してください仕様内でアクセス可能ですが、まだ動作していないと私はまた、プレーンURLで試してみて同じエラーが発生しました。 – Sinscary

  • +0

    返事ありがとう、私は問題が何かを考え出した。それは私が返すものであり、URLではないという私の反応でした。とにかく返信+1。 – Sinscary

    関連する問題