首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebMock.disable_net_connect!不工作

WebMock.disable_net_connect!不工作
EN

Stack Overflow用户
提问于 2015-07-09 18:52:20
回答 1查看 1.9K关注 0票数 6

我正在尝试编写一个基于WebMock的测试用例来模拟调用http。为此,我在我的spec_helper.rb文件中包括了spec_helper.rb,并添加了WebMock.disable_net_connect!(allow_localhost: true)以禁止通过web进行http请求。但是,当我运行一个虚拟测试来检查http请求是否被阻塞时,我可以看到http请求仍然被发出。

spec_helper.rb文件:

代码语言:javascript
复制
ENV["RAILS_ENV"] ||= 'test'
require 'rubygems'
require File.expand_path("../../config/environment", __FILE__)
require 'authlogic/test_case'
include Authlogic::TestCase
require 'rspec/rails'
require 'rspec/autorun'
require 'rspec/mocks'
require 'capybara/rspec'
require 'capybara/rails'
require "paperclip/matchers"
require 'vcr'
require 'webmock/rspec'
WebMock.disable_net_connect!
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.mock_with :rspec
  config.use_transactional_fixtures = false

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.include Paperclip::Shoulda::Matchers

  config.include FactoryGirl::Syntax::Methods

  config.infer_base_class_for_anonymous_controllers = false
  config.include Rails.application.routes.url_helpers
  config.include Capybara::DSL
  config.render_views
  config.filter_run focus: true
  config.run_all_when_everything_filtered = true

end
VCR.configure do |c|
  c.cassette_library_dir = 'spec/vcr_cassettes'
  c.hook_into :webmock
  c.allow_http_connections_when_no_cassette = true
end

ActiveSupport::Dependencies.clear

另外,我编写的虚拟测试文件:

代码语言:javascript
复制
require 'spec_helper'  
describe 'External request' do
    it 'queries FactoryGirl contributors on GitHub' do
      uri = URI('https://api.github.com/repos/thoughtbot/factory_girl/contributors')

      response = Net::HTTP.get(uri)

      expect(response).to be_an_instance_of(String)
    end
  end

请帮助我找出我是否错过了一些配置,或有其他的东西,我正在这样做。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-10 21:36:14

VCR吐露中发现了以下配置问题:

代码语言:javascript
复制
c.allow_http_connections_when_no_cassette = true

将其转换为false解决了问题,因为VCR吐露正在覆盖webmock吐露,因为我已经定义了c.hook_into :webmock

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31325906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档