Webmock和VCR,如果没有磁带,则允许Http Connections

时间:2015-01-29 17:12:17

标签: ruby-on-rails rspec rails-engines vcr webmock

我有一个问题,我可以运行一个单独使用vcr的测试并且它可以工作,它会创建卡带并在下一次测试中使用它。大。

问题是,当我一起运行所有测试时,这个特定的测试失败了,因为webmock禁用了http连接,我看到this example on the Github repo page that explains how to expect real and not stubbed requests

我的问题是如何说:允许Http连接请求,除非有磁带。它还应该在允许HTTP连接时创建磁带。

录像机设置

require 'vcr'

VCR.configure do | c |
  if !ARGV.first.nil?
    c.default_cassette_options = { :record => :new_episodes, :erb => true }
    c.filter_sensitive_data('<BLACKBIRD_API_KEY>') {YAML.load(File.read('config/application.yml'))['BLACKBIRD_API_KEY'].to_s}
    c.filter_sensitive_data('<BLACKBIRD_API_URL>') {YAML.load(File.read('config/application.yml'))['BLACKBIRD_API_URL'].to_s}
    c.debug_logger = File.open(ARGV.first, 'w')
    c.cassette_library_dir = 'spec/vcr'
    c.hook_into :webmock
  end
end

以上if语句存在,因为并非每次测试都会创建一个盒式磁带。所以我们希望它们在不需要磁带时运行。

测试

require 'spec_helper'

describe Xaaron::Publishers::Users do

  context "publish created users" do
    before(:each) do
      Xaaron.configuration.reset
      no_user_member_roles_relation

      Xaaron.configuration.publish_to_black_bird = true
      Xaaron.configuration.black_bird_api_url = YAML.load(File.read('config/application.yml'))['BLACKBIRD_API_URL']
      Xaaron.configuration.black_bird_api_key =YAML.load(File.read('config/application.yml'))['BLACKBIRD_API_KEY']
    end

    it "should publish to blackbird" do
      VCR.use_cassette 'publisher/create_user_response' do
        expect(
          Xaaron::Publishers::Users.publish_new_user({user: {
            first_name: 'adsadsad', user_name: 'sasdasdasdsa'  ,
            email: 'asdassad@sample.com', auth_token: 'asdsadasdasdsa'
          }}).code
        ).to eql 200
      end
    end
  end

end

单独运行,创建磁带,因运行webmock所有其他测试运行时失败。

失败

Failure/Error: Xaaron::Publishers::Users.publish_new_user({user: {
 WebMock::NetConnectNotAllowedError:
   Real HTTP connections are disabled. Unregistered request: GET some_site_url_here with headers {'Http-Authorization'=>'api_key_here', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}

   You can stub this request with the following snippet:

   stub_request(:get, "some site url here").
     with(:headers => {'Http-Authorization'=>'some api key here', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}).
     to_return(:status => 200, :body => "", :headers => {})

0 个答案:

没有答案