4

When running rake spec:rcov for a Rails 3 application, the files in the spec/ directory are getting included in the coverage statistics, but I don't want them to be. I want coverage statistics only for my actual application.

In older versions of RSpec, it was possible to customize this with a spec/rcov.opts file with the line --exclude "spec/*" but it seems that file is no longer read by Rspec 2. I tried creating a .rcov file since spec/spec.opts changed to .rspec, but that didn't get read either.

I've found some documentation on how to do this when defining a rake task, but I'd rather not overwrite the provided rake task — it seems like this must be something other people have tried to do as well.

How can I customize the files that are excluded from coverage statistics?

For reference, the versions of all relevant gems I'm using are:

rails (3.0.5)
rake (0.8.7)
rcov (0.9.9)
rspec (2.5.0,)
rspec-core (2.5.1)
rspec-expectations (2.5.0,)
rspec-mocks (2.5.0)
rspec-rails (2.5.0)
Andy Lindeman
  • 12,087
  • 4
  • 35
  • 36
Emily
  • 17,813
  • 3
  • 43
  • 47

1 Answers1

6

From the RSpec Upgrade file:

In RSpec-1, the rake task would read in rcov options from an rcov.opts file. This is ignored by RSpec-2. RCov options are now set directly on the Rake task:

    RSpec::Core::RakeTask.new(:rcov) do |t|
      t.rcov_opts =  %q[--exclude "spec"]
    end

Inspecting the rspec-rails source code I found the library defines the :rcov task and it doesn't seem to exclude the rspec folder.

desc "Run all specs with rcov"
RSpec::Core::RakeTask.new(:rcov => spec_prereq) do |t|
  t.rcov = true
  t.pattern = "./spec/**/*_spec.rb"
  t.rcov_opts = '--exclude /gems/,/Library/,/usr/,lib/tasks,.bundle,config,/lib/rspec/,/lib/rspec-'
end

You might want to remove the task and recreate it with your own settings or define a new task.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • Setting the options when defining a new rake task is fairly simple -- I'm looking for a way to customize the existing task like you could with Rspec 1. Although I guess this is what I'll be doing if it turns out that's not possible. – Emily Mar 17 '11 at 14:39
  • You can't. You have to unload the existing task and redefine it. – Simone Carletti Mar 17 '11 at 14:40
  • Thanks for the confirmation that this is the way to go. Too bad they removed the ability to customize it. – Emily Mar 17 '11 at 15:01