Making sure rspec + rcov report on all your code

Wed, 01 Oct 2008 | Comments

One of the things that easily catches people out with rcov is that it doesn’t show you the coverage of all your code, it just shows the coverage of the code touched by your tests. So, if you completely forget to add tests for a new controller, that won’t appear in your coverage report.

Here’s an easy fix: save the following code as spec/app_spec.rb:

require File.dirname(__FILE__) + '/spec_helper'

dir = File.dirname(__FILE__)
Dir[File.expand_path("#{dir}/../app/**/*.rb")].uniq.each do |file|
  require file
end

It simply includes all the ruby files under your /app directory so that they’re reported properly by rcov.