Why not use shared ActiveRecord connections for Rspec + Selenium?

后端 未结 6 1855
傲寒
傲寒 2020-11-30 23:12

It seems the most commonly accepted way to deal with Selenium and tests is to avoid using transactional fixtures and then using something like database_cleaner between tests

6条回答
  •  粉色の甜心
    2020-11-30 23:24

    I have encountered a problem using the code you mentioned in my spec_helper.rb file.

    What happens when your tests depend on using connections to multiple databases? I have two databases I need to connect to when I run my tests. I did a simple test to check what was happening to the database connections I establish.

    class ActiveRecord::Base
       mattr_accessor :shared_connection
       @@shared_connection = nil
    
       def self.connection
         @@shared_connection || retrieve_connection
      end
    end
    
    # Forces all threads to share the same connection. This works on
    # Capybara because it starts the web server in a thread.
    puts "First Record cxn: #{FirstDatabase::Record.connection}"
    # => First Record cxn: #
    puts "AR Base cxn: #{ActiveRecord::Base.connection}"
    # => AR Base cxn: #
    ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
    
    puts "First Record cxn: #{FirstDatabase::Record.connection}"
    # => First Record cxn: #
    puts "AR Base cxn: #{ActiveRecord::Base.connection}"
    # => AR Base cxn: #
    

    As you can see, before I call the shared connection method, I have two different database connections. After, the shared connection method call, I have only one.

    So any test that requires going to the second database connection to retrieve information will fail. :(

    I'm going to post this problem and see if anyone has arrived at a solution.

提交回复
热议问题