Travis CI 构建失败并出现 RSpec 错误

Travis CI failing to build with RSpec errors

我的 Travis CI 构建在尝试 运行 RSpec 时失败。这是我的 .travis.yml:

language: ruby
script:
- export RAILS_ENV=test
- bundle exec rake db:create db:schema:load db:test:prepare
- bundle exec rake cucumber
- bundle exec rspec

前三个脚本步骤成功完成,我得到 Done. Your build exited with 0. (here)

但是当我添加第四步时 (bundle exec rspec) 我得到 Done. Your build exited with 1. (here)

构建中的错误 (uninitialized constant CommentsController (NameError)) 来自 "spec/" 文件夹 (comments_controller_spec.rb) 中第一个文件的第一行。以下是 Travis 的错误详细信息:

$ bundle exec rspec
/home/travis/build/deeprog/goalify/spec/controllers/comments_controller_spec.rb:1:in `<top (required)>': uninitialized constant CommentsController (NameError)
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/bin/rspec:23:in `load'
from /home/travis/build/deeprog/goalify/vendor/bundle/ruby/2.2.0/bin/rspec:23:in `<main>'
The command "bundle exec rspec" exited with 1.

我对这个错误感到困惑。我尝试将 require 'rails-helper'/require 'spec_helper' 添加到规范的顶部,但这没有帮助。我也试过 运行ning rake 而不是 bundle exec rspec,但它给出了同样的错误。

该应用程序当前位于相对根目录 ('/goalify'),因此要在本地进行 运行 的测试,我必须在 test.rb 中设置 config.relative_url_root = nil。但是删除那条线也不能解决 Travis 上的问题。我还向 Travis 添加了任何必需的环境变量。

这里有更多信息:

Gemfile:

group :development, :test do
  gem 'byebug'
  gem 'cucumber-rails', require: false
  gem 'database_cleaner'
  gem 'factory_girl_rails'
  gem 'rspec-rails'
  gem 'simplecov', require: false
  gem 'spring'
  gem 'spring-commands-rspec'
  gem 'travis'
end

.rspec

--color
--format documentation
--require spec_helper
--require rails_helper

这里是 source on GitHub.

感谢@sam-d 指出答案!

我将 require 'rails_helper' 添加到 comments_controller_spec.rb,它开始工作了。我认为这很奇怪,因为我在本地不需要额外的 require。然后我意识到我的 .rspec 文件(我将 requires 发送到 RSpec 的地方)在我的 .gitignore 文件中 - 所以 Travis 没有看到包含。我将所有开关添加到脚本部分的第四步,所以我的新 travis.yml 是:

script:
- export RAILS_ENV=test
- bundle exec rake db:create db:schema:load db:test:prepare
- bundle exec rake cucumber
- bundle exec rspec --color --format documentation --require spec_helper --require rails_helper

现在我从 Travis 那里得到 The command "bundle exec rspec --color --format documentation --require spec_helper --require rails_helper" exited with 0.