Search code examples
ruby-on-railsrubyruby-on-rails-3rspec2rcov

Skipping files in spec/ when running rcov with RSpec 2.5


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)

Solution

  • 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.