1
votes

My Travis CI builds are failing when it tries to run RSpec. Here is my .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

The first three script steps complete successfully and I get Done. Your build exited with 0. (here)

But when I add the fourth step (bundle exec rspec) I get Done. Your build exited with 1. (here)

The error in the build (uninitialized constant CommentsController (NameError)) comes from the first line in th first file in the "spec/" folder (comments_controller_spec.rb). Here are the error details from 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.

I am stumped about this error. I have tried adding require 'rails-helper'/require 'spec_helper' to the top of the spec, but that didn't help. I've also tried running rake instead of bundle exec rspec, but that gives the same error.

The app currently lives at a relative root ('/goalify'), so to get the tests to run locally I had to set config.relative_url_root = nil in test.rb. But removing that line doesn't fix it on Travis, either. I've also added any required environment variables to Travis.

Here is some more info:

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

And here is the source on GitHub.

1
add require 'rails_helper' on the first line of github.com/deeprog/goalify/blob/master/spec/controllers/…Sam
@sam-d I've tried that before but it worked right away this time! Make it an answer and I'll accept it - thanks!crgolden

1 Answers

2
votes

Thanks to @sam-d for pointing out the answer!

I added require 'rails_helper' to comments_controller_spec.rb and it started working. Which I thought was weird because I didn't need that extra require locally. Then I realized my .rspec file (where I send my requires to RSpec) is in my .gitignore file - so Travis wasn't seeing the includes. I added all the switches to the fourth step in the scripts section so my new travis.yml is:

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

And now I get The command "bundle exec rspec --color --format documentation --require spec_helper --require rails_helper" exited with 0. from Travis.