← Back to Upcase

How to setup Rspec for CircleCI?

I have been following TDD for a new project and am loving it so far. All of my tests pass locally. However, I am trying to use CircleCI and am having problems getting my tests to run there. From my research and the error messages I have seen so far I think I have something wrong with my Rspec setup that is causing trouble with CircleCI. I am not quite sure how to troubleshoot this. My app is Rails 4.2 and Ruby 2.2. If you have any advice or suggestions I appreciate it.

spec_helper.rb

require "webmock/rspec"
require "capybara/rspec"
require "capybara/rails"

# http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
  config.expect_with :rspec do |expectations|
    expectations.syntax = :expect
  end

  config.mock_with :rspec do |mocks|
    mocks.syntax = :expect
  end

  config.order = :random
end

WebMock.disable_net_connect!(allow_localhost: true)

rails_helper.rb

ENV["RAILS_ENV"] = "test"

require File.expand_path("../../config/environment", __FILE__)

require "rspec/rails"
require "shoulda/matchers"

Dir[Rails.root.join("spec/support/**/*.rb")].each { |file| require file }

module Features
  # Extend this module in spec/support/features/*.rb
  include Formulaic::Dsl
end

RSpec.configure do |config|
  config.include Features, type: :feature
  config.infer_base_class_for_anonymous_controllers = false
  config.infer_spec_type_from_file_location!
  config.use_transactional_fixtures = false
end

ActiveRecord::Migration.maintain_test_schema!
Capybara.javascript_driver = :webkit

The primary error I have been getting is (there is a long trace that followed):

bundle exec rspec --color --require spec_helper spec --format progress 
/home/ubuntu/burger-club/vendor/bundle/ruby/2.2.0/gems/capybara-
2.4.4/lib/capybara/rails.rb:6:in `block (2 levels) in <top (required)>': 
uninitialized constant Rails (NameError)

Your spec_helper.rb refers to capybara/rails, but Rails is loaded only by rails_helper.rb (on line 3).

If you run a single spec file individually which doesn’t require 'rails_helper' then you should see it fail with the same error that you report above.

If you run multiple specs (e.g. the whole suite), everything will appear to work ok as long as rails_helper gets loaded before spec_helper.

I suspect the reason the suite is passing for you locally is because the files are getting read in a different order due to CircleCI running a different OS. Or it could be because RSpec is set to run the specs in a random order, and you’ve just been lucky so far when running them locally.

Regardless, here’s how to fix it:

Capybara is generally only used for feature specs, so you probably need to move the following lines from spec_helper to rails_helper:

require "capybara/rspec"
require "capybara/rails"

Also, the line for including url_helpers should be in the the rails_helper, not the spec_helper.

Make sure that each of your specs begins with require 'rails_helper' unless you know for sure that it has no dependencies on Rails.

The rspec-rails docs explain the difference between the two helper files:
https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

2 Likes

Thank you for the clear and detailed answer. I learned so much from this, all my tests are passing, and CircleCI is working beautifully. Much appreciated.