[rspec-users] stale records with integration testing?

David Chelimsky dchelimsky at gmail.com
Tue Feb 14 06:31:32 EST 2012

On Tue, Feb 14, 2012 at 1:36 AM, Patrick J. Collins
<patrick at collinatorstudios.com> wrote:
>> > 2.) DatabaseCleaner:
>> >
>> > RSpec.configure do |config|
>> >  config.before { DatabaseCleaner.start }
>> >  config.after { DatabaseCleaner.clean }
>> > end
>> >
>> > Look into what those do. Let us know if you get stuck.
>> What Justin says is true if you're running in the same process. If
>> you're using Capybara to run examples in-browser, then option 2 will
>> work for you, but option 1 will not.
> Hmmm..  Well, I had actually tried the DatabaseCleaner gem prior to
> asking my question, and I saw no difference in the results...  I just
> put that code back in spec_helper, and ran my specs and they fail again
> with "email already taken".

This could happen for one of two reasons:

1. The email is already in the database before you run your specs.

To avoid this, use DatabaseCleaner to truncate the tables once before
each run (not before each example).

2. Models are generated in before(:all) blocks, which are not
implicitly wrapped in transactions.

If you generate models in before(:all), then it is your responsibility
to remove them in an after(:all).

Also - DatabaseCleaner has two modes: transaction and truncation. Make
sure you're using the appropriate one.


More information about the rspec-users mailing list