Is DatabaseCleaner still necessary with Rails system specs?
Asked Answered
A

1

41

From all that I've read about Rails 5.1 new system specs my understanding was that Rails now handles database transactions internally.

From Rspec's blog: "[previously] your tests and your code under test cannot share a database transaction, and so you cannot use RSpec's built in mechanism to roll back database changes, instead requiring a gem like database cleaner. With system tests, the Rails team has done the hard work to ensure that this is not the case, and so you can safely use RSpec's mechanism, without needing an extra gem."

My experience is different:

  1. My Rspec feature tests were all passing after upgrading to Rails 5.1.
  2. I renamed the 'feature' specs to become 'system' specs. All tests passed.
  3. I removed the Database Cleaner gem, and removed all references from rails_helper.rb. JS tests now fail due to validates uniqueness errors. Non-JS tests pass.

My tests are very simple.

let(:subject) { page }
let(:user) { create :user, name: "TestUser" }
it "displays the user page", :js do
  visit user_path(user)
  it is_expected.to have_content "TestUser"
end

With Database cleaner disabled and :js => true I get user named TestUser already exists. With :js => false the test passes.

What is the current situation with system tests and rspec? Does Rails handle database transactions internally, or is Database Cleaner still required? Has anyone encountered this, or can point me towards relevant info?

Ampulla answered 12/3, 2018 at 23:45 Comment(3)
Their wording was hard for me to understand as well... I read it as - use rails system tests -i.e. under test/system instead - or at least inheriting from ActionDispatch::SystemTest . I wrote up my experience here: dev.to/dstull/…Quickstep
thanks @dstull, actually your write up has already been a useful reference! I've successfully swapped to system tests and all is working. Now I'm trying to optimise, and had hoped to remove the Database Cleaner dependency from my set up. But not sure whether I'm misunderstanding, and whether this is still required!Ampulla
actually system specs! I'm using rspecAmpulla
T
58

You do not need the DatabaseCleaner gem. Here's a quick summary of steps you need:

  1. Remove capybara-selenium gem and database_cleaner gem
  2. Add selenium-webdriver gem
  3. Make sure you are using Rails 5.1.5 or later. Earlier versions of Rails 5.1 had a defect in ActionDispatch::SystemTesting::Server that caused problems (fixed here).
  4. In your rails_helper file, set config.use_transactional_fixtures = true
  5. If you are using Devise for auth, in your rails_helper file, set config.include Devise::Test::IntegrationHelpers, type: :system
  6. Set up a basic_configure file as per this post by Noel Rappin.
  7. Replace RSpec.feature throughout with RSpec.describe.

See this commit for more details as to how I managed the switch. It was a hassle, but hopefully easier now that 5.1.5 has fixed the ActionDispatch::SystemTesting::Server issue (I had to monkey patch the file in 5.1.4, but you won't have to now).

Territorialism answered 13/3, 2018 at 4:3 Comment(1)
So such a stupid issue, can't believe I overlooked it! I think the root of my problem was that I had not dropped capybara-selenium in favor of selenium-webdriver. Anyway, all is now behaving as expected. Thanks for getting me in the right direction!!Ampulla

© 2022 - 2024 — McMap. All rights reserved.