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:
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?
You do not need the DatabaseCleaner gem. Here's a quick summary of steps you need:
capybara-selenium
gem and database_cleaner
gemselenium-webdriver
gemrails_helper
file, set config.use_transactional_fixtures = true
config.include Devise::Test::IntegrationHelpers, type: :system
basic_configure
file as per this post by Noel Rappin.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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With