RCov with RSpec 2

By Morten Møller Riis

March 01 2011 10:30 CET

When writing a test/spec suite for your app it can sometimes be a good idea to use rcov to get an idea of the coverage of your tests/specs.

I’ve had some trouble using rcov with RSpec 2 so I thought I might as well post my solution here.

For my project (Rails 3, RSpec 2) I had to add the following Rake task.

lib/tasks/rcov.rake

               require 'rspec/core/rake_task'
               
               desc "Run all specs with rcov"
               RSpec::Core::RakeTask.new("spec:coverage") do |t|
                 t.rcov = true
                 t.rcov_opts = %w{--rails --include views -Ispec --exclude gems\/,spec\/,features\/,seeds\/}
                 t.spec_opts = ["-c"]
               end
             

After creating this file you should be able to do a simple rake spec:coverage and find the coverage report in coverage/.