0

I'm fairly new to RSpec and have been trying to create some tests for my website, on which a user can post a reservation to the website, which is then saved to our database. I've been trying, using Rspec and Capybara, to simulate a user posting a reservation to the website. We have an existing test database, and at the end of the Rspec test want the new reservation to be written to the database, and not removed at the end of the Rspec test.

One of two things happens when we run the code: either it "works" but the new reservation can't be found in the database, or we get this error:

Failure/Error: Unable to find matching line from backtrace
 ActiveRecord::StatementInvalid:
   Mysql2::Error: This connection is in use by: #<Thread:0x007fb421fd6218 sleep>: SELECT `users`.* FROM `users`  WHERE `users`.`id` = 6  ORDER BY `users`.`id` ASC LIMIT 1
 # ./app/controllers/application_controller.rb:95:in `pass_login_status_to_js'
 # ./app/middleware/search_suggestions.rb:12:in `call'

Why would this be happening? I realize that Capybara isn't generally meant to be making permanent changes to a database; is there a different program/gem you recommend?

I currently have config.use_transactional_fixtures = false, and also have added the following on the recommendation of a few websites: class ActiveRecord::Base mattr_accessor :shared_connection @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection 

To reiterate, I do want Capybara to be writing to my database (we use SQL). What can I do differently? Does it have something to do with database cleaner?

amitmaor
  • 1
  • 1

1 Answers1

0

Yes, it has everything to do with database_cleaner. If you have it setup properly, it will clean your database between scenarios, to keep the tests isolated.

There are a few ways to do what you want:

  1. You can explicitly tell database_cleaner not to clean certain tables between scenarios:

    DatabaseCleaner.strategy = :transaction, {except: [:countries, :states]} DatabaseCleaner.clean_with(:truncation, {except: [:countries, :states]})

  2. You can add your code to a before(:each) or before(:all) block

  3. You can add your data to one or many fixtures

There are only a few cases where you should share data between scenarios (ie. countries, states tables, which are good candidates for #3)

In any other case, I advise against sharing data between scenarios.

etagwerker
  • 523
  • 4
  • 15