Mahoni
Mahoni

Reputation: 7466

Capybara feature tests does not commit database transactions

I am using Machinist to create my test users which I want to log in before each test which I am running with Capybara:

include Capybara::DSL
include ActionController::UrlWriter

before do
  SslRequirement.disable_ssl_check = true
  user = User.make


  visit new_user_session_path
  fill_in('username', with: user.email)
  fill_in('password', with: '12345')
  click_button('submit')
end

it "responds as expected" do
   # ...
end

Now when the user is created with User.make I can access it through User.all when it fact it's not persisted in the database yet, this is because

ActiveRecord::Base.connection.open_transactions == 1

I can fix/hack this by doing a

ActiveRecord::Base.connection.commit_db_transaction

after User.make, but I'd rather fix this properly. Is this a Capybara configuration which I am missing? Disclaimer: Using Rails 2.3 and RSpec 1.3

Upvotes: 1

Views: 827

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

You don't show which driver you're using with Capybara, but generally you shouldn't be using transactions for testing when using Capybara (yes there are potential workarounds to allow it but they all have side-effects). See transactions and database setup and then look into the database_cleaner gem with truncation - here's a blog post about it that should be good with the age of gems you're using - http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

Upvotes: 2

Related Questions