blog.mhartl | Michael Hartl's tech blog

2009-05-15

Running rcov with RSpec

Filed under: RSpec, Ruby, Ruby on Rails — mhartl @ 16:01

I recently wanted to run rcov, the Ruby code coverage tool, on a project tested with RSpec. I think I’d done it once before, but I’d forgotten how. After searching to no avail (both the rcov home page and the RSpec page on rcov proved unhelpful), I applied the tried-and-true Wild-Assed Guess™ method and typed

$ rake spec:rcov

That worked.

The reports themselves are in the coverage/ directory:

$ open coverage/index.html

(or navigate your browser to file:///path/to/project/coverage/index.html). If you’re using Git, add coverage/* to your .gitignore file.

N.B. The inverse Rake task also exists:

$ rake -T rcov
rake spec:clobber_rcov  # Remove rcov products for rcov
rake spec:rcov          # Run all specs in spec directory with RCov (excluding plugin specs)

2008-07-28

Running Rails tests with autotest (ZenTest) and RSpec

Filed under: autotest, RSpec, Ruby on Rails — mhartl @ 13:12

I recently ran into a problem with autotest (ZenTest) after upgrading to Rails 2.1 and RSpec 1.4.1. Solving it was annoying, so I hope I can save others some trouble. Here’s the problem:

With RSpec, autotest hangs

Before the upgrade, I could run my specs just fine using the plain autotest command, but after the upgrade autotest just hangs:

$ autotest
loading autotest/rails

This is on a system running Mac OS X Tiger (10.4), Rails 2.1.0, RSpec 1.4.1, and ZenTest 3.10.0. Strangely, my friend Long could run autotest fine on a virtually identical system (so you may not run into this problem), but for me this only increased the frustration. After much hand-wringing (and a lot of Google searching), I finally found a Rails Forum post with a solution:

$ RSPEC=true autotest

Then autotest runs normally.

Restoring the old RSpec/autotest behavior

To get the old behavior, you can include the RSPEC variable in your environment rather than putting it explicitly on the command line. For example, on a system running bash, export the RSPEC variable as follows:

file: ~/.bashrc

export RSPEC=true

Then source it:

$ . ~/.bashrc

Now autotest should run as before:

$ autotest

Voilà (I hope)!

UPDATE: Since making this post, I’ve learned that RSpec now ships with a program called autospec that solves the same problem; just run

$ autospec

and the specs should run as expected.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.