【问题标题】:cannot load such file -- capybara/minitest无法加载此类文件 -- capybara/minitest
【发布时间】:2017-07-19 21:55:05
【问题描述】:

希望有人可以帮助我。我进行了搜索,但没有找到任何可行的解决方案。

我已经开始为一个应用编写测试。我的集成测试运行良好,但后来我决定,因为我不太受 TDD 驱动,而且我现在没有太多时间来广泛测试我应该使用而不是 integration 的应用程序的所有层测试system 测试,因为它们允许我像在浏览器中一样测试整个流程。

Rails 5.1.2

Gemfile (尝试了不同的变体,只是水豚,然后是其他两种的组合)

gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'capybara'

test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  EMPTY_NEW_USER = {
    email: '',
    first_name: '',
    last_name: '',
    username: '',
    password: ''
  }

  EXISTING_USER = {
    email: '****',
    first_name: 'John',
    last_name: 'Doe',
    username: '',
    password: 'testingpass',
    password_confirmation: 'testingpass'
  }

  # Add more helper methods to be used by all tests here...
end

application_system_test_case.rb

require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end

register_logins.rb

require "application_system_test_case"

class RegisterLoginsTest < ApplicationSystemTestCase

  test 'full login flow' do
    visit root_url
    assert_response :success

    find('.email_link').click


  end
end

运行时出错

rake test:system

LoadError: cannot load such file -- capybara/minitest
/Users/mnussbaumer/code/dvouch/test/application_system_test_case.rb:3:in `<top (required)>'
/Users/mnussbaumer/code/dvouch/test/system/register_logins_test.rb:1:in `<top (required)>'
Tasks: TOP => test:system
(See full trace by running task with --trace)

完整的跟踪添加了这个:

LoadError: cannot load such file -- capybara/minitest
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `block in require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
/Users/mnussbaumer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-5.1.2/lib/action_dispatch/system_test_case.rb:2:in `<top (required)>'
/Users/mnussbaumer/code/dvouch/test/application_system_test_case.rb:3:in `<top (required)>'

并继续 active_support 依赖项。

我尝试过的:

将一、二、三加到test_helper.rb

require "capybara/rails"
require "minitest/rails"
require "minitest/rails/capybara"

我尝试过使用宝石:

group :development, :test do
  gem 'minitest-rails'
  gem 'minitest-capybara'
  gem 'capybara'
end

然后用'minitest-rails-capybara'

谢谢

【问题讨论】:

  • 你用的是什么版本的水豚?
  • 水豚 (2.6.2) @ThomasWalpole
  • Rails 5.1 系统测试至少需要 2.13.0(最好使用最新的 (2.14.4),然后不再需要 minitest-capybaraminitest-rails gems - 你将需要 'selenium -webdriver' 用于默认设置
  • 谢谢@ThomasWalpole,解决了它,只需要在战后添加 chromedriver 就可以了。请添加它作为答案,我会接受它。是的,assert_response 没用。顺便说一句,也许您也可以向我提供您的意见,是否有任何重要的理由使用硒而不是无头浏览器?我只运行了这 4 行代码,它就像永恒
  • Selenium 运行的是现代浏览器,无头浏览器驱动程序(capybara-webkit、poltergeist)现在都不支持 ES5 之外的任何东西,并且还有一些其他限制(视频、音频等)。因为使用 selenium 是用户的更“正确”表示。您现在还可以使用无头 Chrome 运行 selenium,这并没有增加太多速度,但希望将来会增加。此外,运行一项测试会产生很多开销,当您进行多项测试时,这将摊销。

标签: ruby-on-rails capybara minitest system-testing


【解决方案1】:

文件 capybara/minitest 在 2.13.0 版本中添加到 Capybara,这是 Rails 自 Rails 5.1.0 以来系统测试所需的最低版本。升级到最新版本的 Capybara (2.14.4),应该不需要 minitest-capybaraminitest-rails gems。您还需要将“selenium-webdriver”gem 添加到您的测试组。

此外,assert_response :success 行在 Capybara 测试中无效,因为来自 Capybara 正在使用的浏览器的 HTTP 响应代码通常不可用。

【讨论】:

  • 谢谢,我添加了一条评论,如果您愿意分享您对使用 selenium 或无头硒的意见,例如 poltergeist/cb-webkit,我将不胜感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 2015-08-16
  • 1970-01-01
  • 2016-11-20
  • 2023-03-10
  • 2015-11-20
相关资源
最近更新 更多