首页 > 解决方案 > 运行系统测试的新 Rails 项目出现 loaderror

问题描述

未经测试创建的 Rails 新项目 (6.1)

rails new [project name] -T 

然后我安装了 rspec 5 并生成了一个输出以下错误的系统测试。

LoadError: cannot load such file -- 
capybara System test integration requires Rails >= 5.1
and has a hard dependency on a webserver and `capybara`,
please add capybara to your Gemfile and configure a webserver
(e.g. `Capybara.server = :webrick`) before attempting to use system specs.
No examples found.

标签: ruby-on-railsrspeccapybara

解决方案


如果您在 Gemfile 中缺少 Capybara gem,如错误所示,您将收到此 LoadError。

如果您在没有测试的情况下生成 rails rails new [project] -T,然后安装 RSpec,则可能会发生这种情况,因为它不会在 Gemfile 中添加 Capybara。

解决方案

添加一个测试组并将 capybara 放入其中可以让您像这样克服这个错误。

# Gemfile
group :test do
  gem 'capybara', '>= 2.15'
end

推荐阅读