Webmock未注册请求将我的测试炸开



在我的 spec_helper中,我的网络货物设置如下:

WebMock.disable_net_connect!(allow_localhost: true)
RSpec.configure do |config|
  config.before(:each) do
    stub_request(:get, /cdn.w+.io/).
      with(:headers => {'Accept-Encoding'=>'gzip', 'Access-Token'=> ENV['ACCESS_TOKEN'], 'Api-Key'=>ENV['API_KEY'], 'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}).
      to_return(:status => 200, :body => "", :headers => {})
  end
end

现在,当我使用bundle exec rspec spec/stack_client_query_spec.rb单独运行此测试时,它通过。

  describe "can query for entries by asking for a content_type" do
    it "returns a Typhoeus response with queried with a content_type" do
      response = create_client.content_type(content_type_uid).get
      expect(response).to be_instance_of(Typhoeus::Response)
    end
  end

但是,当我使用bundle exec rspec运行完整的测试套件时,相同测试随着此输出而爆炸:

Failure/Error:
       response = Typhoeus::Request.new(
         endpoint,
         headers: { api_key: headers[:api_key], access_token: headers[:access_token],
         accept_encoding: "gzip" }
       ).run
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://cdn.mycustom.domain/v3/content_types/shirts/entries/ with headers {'Accept-Encoding'=>'gzip', 'Access-Token'=>'bltbdc42da30987971c', 'Api-Key'=>'blt1c501b5fa4b64377', 'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}
       You can stub this request with the following snippet:
       stub_request(:get, "http://cdn.mycustom.domain/v3/content_types/shirts/entries/").
         with(:headers => {'Accept-Encoding'=>'gzip', 'Access-Token'=>'bltbdc42da30987971c', 'Api-Key'=>'blt1c501b5fa4b64377', 'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}).
         to_return(:status => 200, :body => "", :headers => {})
       registered request stubs:
       stub_request(:get, "/cdn.w+.io/").
         with(:headers => {'Accept-Encoding'=>'gzip', 'Access-Token'=>'bltbdc42da30987971c', 'Api-Key'=>'blt1c501b5fa4b64377', 'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'})

似乎较早的测试正在覆盖请求域或其他内容。我不确定为什么!我究竟做错了什么?我该如何解决?

看起来您有固执

stub_request(:get, /cdn.w+.io/)

但实际请求将

http://cdn.mycustom.domain/

您可以将存根更改为

stub_request(:get, /cdn.w+.domain/)

最新更新