I just installed the SimpleCov gem to generate code coverage reports on my Rails 3.2.6 app, and it works great with RSpec, just not with Spork. I am able to get the desired correct report by running rspec --no-drb spec/, but I’d like to also get them with Spork running using just rspec spec/.
Given that there have been people who have had success with this, it seems likely I have errors in my setup. I have read through the setup instructions as well as the GitHub issue that purports to have a fix for Spork users, but still no luck. I’m wondering if there is anyone who can provide a full example of their working spec/spec_helper.rb file that I could use for reference, as extensive Googling has only turned up snippets. On the advice of other sites, I’ve tried changing the config.cache_classes in config/environments/test.rb from the default of true to false to !(ENV['DRB'] == 'true'), with no luck.
For reference, this is how I’m setup:
Gemfile
group :development, :test do
# ...
gem 'rspec-rails', '2.10.1'
end
group :test do
# ...
gem 'spork', '0.9.0'
gem 'simplecov', '0.6.4', require: false
end
.spec
--colour
--drb
spec/spec_helper.rb (changed as per the GitHub issue)
require 'simplecov'
SimpleCov.start 'rails'
require 'rubygems'
require 'spork'
Spork.prefork do
unless ENV['DRB']
require 'simplecov'
SimpleCov.start 'rails'
end
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
end
end
Spork.each_run do
if ENV['DRB']
require 'simplecov'
SimpleCov.start 'rails'
end
end
I’ve tried commenting out/changing the top two SimpleCov statements of this file and the Simplecov statements inside the Spork blocks, but can’t seem to find a combination that works.
What am I missing? Are there any other files I need to change?
I managed to get a working spec/spec_helper.rb configuration that executes SimpleCov correctly simply using the
$ rspec spec/command thanks to a comment on the Github issue that sent me to this blog entry, and its example spec/spec_helper.rb. All reasons why this works are contained in the (very detailed!) blog entry. ReplaceSampleAppwith the name of your application.spec/spec_helper.rb
Edit
If you use Travis-CI, don’t use this code as-is because you’ll likely get a
undefined method 'root' for Rails:Module (NoMethodError)error. If you know how to fix this, please share.Edit 2
I got Travis CI to work by essentially putting everything in the
Spork.each_runblock, which seems to have slowed down the tests significantly. There must be a better way to do this, or it just doesn’t seem worth it for the sake of not having to run$ rspec --no-drb spec/once to get the SimpleCov report…spec/spec_helper.rb
Edit 3
After using this config for a few days, it doesn’t seem to have slowed down things as much as I previously thought, so I’ll consider this the accepted answer unless a more elegant one is posted.
Edit 4
After using this config for a few months, I’ve come to realize that it is slower than I thought. Part of that is the realisation that it seems that Spork can slow down test suites, and is best for fast iterative focused testing vs always running entire test suites with it. The following SO questions and blog posts got me to the spec_helper.rb file below, which can run SimpleCov with or without Spork, runs faster than before, and works with Capybara 2.0.
spec/spec_helper.rb