Search

Reasonable RSpec Config for Clean, and Slightly Faster, Specs

Steven Harman

2 min read

Apr 5, 2012

Reasonable RSpec Config for Clean, and Slightly Faster, Specs

Database-bound tests are a drag. Inconsistent tests are a pain. Database-bound,
inconsistently failing tests are the worst!

The following commit message is from a real code base:

Run in transactions by default.

When we added controller specs they weren’t being run w/any kind of DB
cleaner b/c there was no default strategy and they weren’t explicitly
included in a group. Now, we use :transactions be default, setting request
specs to use :truncation

Also, I saw a 2 second speed up from this change!

myself

Let’s look at what we changed in this commit to turn our inconsistently failing
database-bound tests into slightly faster, consistent, database-bound tests.

Prefer isolated tests

Whenever possible I strive to write isolated tests, stubbing out collaborators
where necessary, while driving from the outside of the system downward, one
layer at time. However, there are occasions where you must hit the database.

Testing ActiveRecord scopes and higher-order acceptance tests are two cases
where I believe it is okay to have tests which cross layers, and may even hit a
database.

Ensuring idempotent tests

I don't always write database-bound tests, but when I do, I prefer them
to be idempotent.

When you are going to hit a database, your
tests should be good citizens and clean up after themselves.

If you’re using RSpec with Rails your tests run within a transaction
by default. Yay!

Unfortunately, test tooling like Capybara won’t work with
transactions and you’ll be forced to resort to techniques like database
truncation to ensure proper data clean up. I really like Database
Cleaner
for that job.

An example RSpec configuration

What follows is the RSpec configuration we ended up with after the previously
mentioned commit. Or at least a very close approximation of it

A few key take aways:

To start, turn off RSpec’s built-in transaction support as we’ll handle that
with database_cleaner.

Next we configure a default clean-up strategy for every RSpec suite. We’ll use
transactions by default, being sure to clean up any thing that might have been
left behind with a truncation.

We make a special exception for request specs, which are often driven by
Capybara, and switch them over to truncation.

Then, before every test runs start Database Cleaner

And finally, after every test runs, tell Database cleaner to clean up using
whatever strategy it is currently configured with.

For posterity’s sake, you can see a full before and after of
this particular spec_helper on The GitHubs.

Mark Dalrymple

Reviewer Big Nerd Ranch

MarkD is a long-time Unix and Mac developer, having worked at AOL, Google, and several start-ups over the years.  He’s the author of Advanced Mac OS X Programming: The Big Nerd Ranch Guide, over 100 blog posts for Big Nerd Ranch, and an occasional speaker at conferences. Believing in the power of community, he’s a co-founder of CocoaHeads, an international Mac and iPhone meetup, and runs the Pittsburgh PA chapter. In his spare time, he plays orchestral and swing band music.

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Not applicable? Click here to schedule a call.

Stay in Touch WITH Big Nerd Ranch News