4

I'm running integration specs using Rspec and Capybara and cleaning out records between specs using Database Cleaner. If it matters, I'm running my specs automatically using Guard and Spork.

Somehow, in the middle of a test run, records are being deleted from the database, causing them to fail. Have I setup Datbase Cleaner incorrectly? Or am I doing something else wrong? I saw this post already, but I don't think this is my problem.

Any help would be appreciated!

Here is spec_helper.rb

Spork.prefork do
  # ...    
  RSpec.configure do |config|
    config.mock_with :rspec
    config.use_transactional_fixtures = true
    config.include(MailerMacros)

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true

    config.before(:suite) { DatabaseCleaner.strategy = :truncation }
    config.before(:each) { DatabaseCleaner.start }
    config.after(:each) { DatabaseCleaner.clean }
  end

end

Spork.each_run do
  FactoryGirl.reload
end

And here is my spec: (notice the 2 puts statements)

feature "Claim Firm" do
  let(:firm) { Factory(:firm, :user_id => nil) }

  scenario "The details page should show a 'Claim' link", :focus => true do
    email = 'claimer@foo.com'
    password = 'secretpass123'
    u = Factory(:user, :email => email, :password => password, :name => "Bob Claimer")
    puts "User Count: #{User.count}" # ==> 1
    visit login_path
    puts "User Count: #{User.count}" # ==> 0
    # The rest of the spec fails because there are no user records...
  end
end
Community
  • 1
  • 1
Adam Albrecht
  • 6,680
  • 4
  • 31
  • 35
  • I'm using SQLite in test – Adam Albrecht Feb 20 '12 at 13:50
  • 1
    Try changing DatabaseCleaner.strategy to :transaction or :deletion? – bdon Feb 20 '12 at 16:33
  • :deletion seems to have worked, but I'm still curious what the root of the problem is. Oh well, this will be fine for now. Thanks! – Adam Albrecht Feb 21 '12 at 23:38
  • Also hit but this behaviour ... did someone find the root of the problem or how to sort it out without changing strategy? – dgilperez May 07 '12 at 15:39
  • No, not that I'm aware of. Using :deletion worked, but it was slower. And on another project, I've been using :truncation without any problem. So it's still a mystery to me. – Adam Albrecht May 07 '12 at 21:07
  • I have the same issue ang using :deletion does not solve it – Sebastien Aug 13 '13 at 19:19
  • Not sure, but maybe it has to do with the let statement? I have the same problem, however, if I use a before block it works. I guess (didn't check it) that let initiates the variable when you call it, not before. Just a guess. – frbl Apr 19 '14 at 16:38
  • hence, using let! fixed it for me ;) – frbl Apr 19 '14 at 16:40
  • This is happening to me too using MYSQL. Just a straight up random deletion of stuff. So strange and really nothing different than other tests... – Greg Blass Sep 01 '15 at 23:56

1 Answers1

1

Not sure what the root cause is, but the truncation strategy for SQLite is an optimization that seems to behave funny in certain situations, so stick with :transaction or :deletion if they're not too slow.

bdon
  • 2,068
  • 17
  • 15