RSpec with Capybara (Feature Specs) integration
mainWhen using Capybara with drivers that use a separate process (like JavaScript-enabled browsers), the application and the test suite do not share a database connection. In these cases, the :transaction strategy will fail because the app cannot see uncommitted data. You must use the :truncation strategy for these feature specs.
Note: Ensure config.use_transactional_fixtures is set to false in Rails to prevent conflicts.
require 'capybara/rspec'
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :feature) do
# Use truncation if the driver doesn't share the DB connection
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
unless driver_shares_db_connection_with_specs
DatabaseCleaner.strategy = :truncation
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
end