0

私は rspec と webmock を使用しており、スタブ要求を検討しています。正規表現を使用して URI を照合しようとすると、問題が発生します。

特定の URI (/.*/)に一致させずに、以下のスタブを使用すると、すべてが正常に機能していました。

it "returns nil and stores an error when the response code is not OK" do
      stub_request(:get, /.*/).
        with(
        :headers => insertion_api.send(:default_headers,  false).merge('User-Agent'=>'Ruby'),
        :body => {}
      ).
       to_return(
        :status => Insertion.internal_server_error.to_i,
        :body => "{\"message\": \"failure\"}",
        :headers => { 'Cookie' => [session_token] }
      )

      expect(insertion_api.get_iou(uid)).to be_nil
      expect(insertion_api.error).to eq("An internal server error occurred")
     end

読みやすさを向上させるためにテストをより具体的にしたいので、 以下のスタブを使用して、次の特定の URI: /insertion_order/012awQQd?fields=name,type&depth=4と一致させようとすると:

it "returns nil and stores an error when the response code is not OK" do
          stub_request(:get, %r{insertion_order/\w+\?fields\=[\w,]+\&depth\=[0-9]}).
            with(
            :headers => insertion_api.send(:default_headers,  false).merge('User-Agent'=>'Ruby'),
            :body => {}
          ).
           to_return(
            :status => Insertion.internal_server_error.to_i,
            :body => "{\"message\": \"failure\"}",
            :headers => { 'Cookie' => [session_token] }
          )

          expect(insertion_api.get_iou(uid)).to be_nil
          expect(insertion_api.error).to eq("An internal server error occurred")
         end

私が持っているテストを実行しています:

WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET https://mocktocapture.com/mgmt/insertion_order/0C12345678 with body '{}' with headers {'Accept'=>'application/vnd.xxx.mgmt+json; version=2.0', 'Cookie'=>'y0Urv3ryLon6s3cur1tYT0k3ng0zeh3r3', 'User-Agent'=>'Ruby'}

       You can stub this request with the following snippet:

       stub_request(:get, "https://mocktocapture.com/mgmt/insertion_order_units/0C12345678").
         with(:body => "{}",
              :headers => {'Accept'=>'application/vnd.dataxu.mgmt+json; version=2.0', 'Cookie'=>'y0Urv3ryLon6s3cur1tYT0k3ng0zeh3r3', 'User-Agent'=>'Ruby'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:get, "/insertion_order\/\w+\?fields\=[\w,]+\&depth\=[0-9]/").
         with(:body => {},
              :headers => {'Accept'=>'application/vnd.xxx.mgmt+json; version=2.0', 'Cookie'=>'y0Urv3ryLon6s3cur1tYT0k3ng0zeh3r3', 'User-Agent'=>'Ruby'})

使用した正規表現は正しいのですが、このエラー メッセージが表示される理由がわかりません。

4

1 に答える 1

0

あなたが受け取ったリクエストは次のとおりです。

https://mocktocapture.com/mgmt/insertion_order/0C12345678

あなたは正規表現を与えました:

%r{insertion_order/\w+\?fields\=[\w,]+\&depth\=[0-9]}

「\?」で指定した正規表現では、リクエストに「?」を含めることは必須です。(またはクエリ) "insertion_order/\w+" の後。取得したリクエストには、クエリ パラメータがありません。そのため、リクエストに一致しません。

これを修正できる 1 つの方法は、正規表現の「insertion_order/\w+」の後に来る部分をオプションにすることです。私はこのようにします:

%r{insertion_order/\w+(\?fields\=[\w,]+\&depth\=[0-9])?}
于 2015-02-24T10:41:10.727 に答える