Issue with cleaning my database after tests (Rspec/Capybara/Poltergeist)
3
Not solved Solved
11 months ago

Hi guys,

I have an issue that drives me crazy. Each time I run my test, the database is not erased at the end. And it keeps accumulating. Which at some point create failed tests.

I'm sure it has something to do with the settings i did this afternoon, to have Poltergeist shared the same db connection with Rspec (otherwise, I couldn't have it worked properly for some reasons).

Here are my settings files :

  • rails_helper: https://gist.github.com/db0sch/6844fe8df2dede0e49773bd28296474a
  • spec_helper: https://gist.github.com/db0sch/003a79efb97d6e04eddb200334cf0efe
  • gemfile : https://gist.github.com/db0sch/09ee59a8974d041ce8aa237407b4348d
  • database_cleaner: https://gist.github.com/db0sch/aa5705e5f7bd9aa237bd3e02307a3265
  • sharedbconnection: https://gist.github.com/db0sch/e0d1e52e9ed067fff085b01fa6d48142

If you guys have an idea, that would be great. These db connection issues are just too complicated for me.

Thanks a lot

11 months ago

Someone much smarter than me once wrote this article

It explains it so much better than I ever could

11 months ago

please not that 'selenium' is an older way of doing javascript testing, so read it as if he is talking about poltergeist and PhantomJS instead of selenium and a browser

11 months ago

gem 'database_cleaner', '~> 1.4.1', require: false in the test group, and something like this in the rails_helper:

RSpec.configure do |config|
  config.before :suite do
    DatabaseCleaner.clean_with :truncation
    DatabaseCleaner.strategy = :truncation, { except: config.seed_tables }
    Rails.application.load_seed
  end
  config.before :example do |example|
    if example.metadata[:js] || example.metadata[:type] == :feature
      DatabaseCleaner.strategy = :deletion, { except: config.seed_tables }
    end
    DatabaseCleaner.start
  end
  config.after :example do |example|
    DatabaseCleaner.clean
  end
end```
11 months ago

Wow !!! Thank you so much guys. I'll try that tomorrow (I'm in Japan, it's already the end of the day here). Thx a lot

11 months ago

It seems that the file database_cleaner.rb isn't loaded in your rails_helper.

I think uncommenting the line #28 in rails_helper.rb -> # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } should solve your problem.

But your database_cleaner.rb file should match the provided path, so be sure to put it in specs/support/database_cleaner.rb

Hope this helped ;)

11 months ago

@kevcha : you found it ! It was just this... Sorry. After a full day of coding, I couldn't see straight. Even when it's obvious like this. Thank you all. I will have a look at the article you posted @arnoFleming

Cancel
Submit your answer