0

I am using a memory sqlite database to run rspec tests. This functions very well. Only when running selenium driven tests (describe "does something", :js => true do) the starting webbrowser gets the error SQLite3::SQLException: no such table: users: SELECT "users".* FROM "users" WHERE ... WEBrick/1.3.1 (Ruby/2.0.0/2013-02-24) at 127.0.0.1:57827 I am looking for a solution to run selenium driven test while using a in memory database.

Details:

I use ruby on rails 4.0 and the following gems (excerpt)

gem 'sqlite3', '1.3.7'
gem 'rspec-rails', '2.13.0'
gem 'capybara', '~> 2.1.0.beta1'
gem 'selenium-webdriver', '2.35.1'

database.yml

test:
    adapter: sqlite3
    database: ":memory:" 
    pool: 5
    timeout: 5000

spec_helper.rb

require 'rubygems'
require 'spork'
Spork.prefork do
     # snip
    load "#{Rails.root.to_s}/db/schema.rb"  # set up memory db
    RSpec.configure do |config|
    config.use_transactional_fixtures = false #using database cleaner
    #snip
    config.before :suite do
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end

    config.before type: :request do
       DatabaseCleaner.strategy = :truncation
    end

    # Reset so other non-request specs don't have to deal with slow truncation.
    config.after type: :request do
       DatabaseCleaner.strategy = :transaction
    end
    config.before(:each, :js => true) do
       DatabaseCleaner.strategy = :truncation
    end
    config.before do
       DatabaseCleaner.start
       ActionMailer::Base.deliveries.clear
    end

    config.after do
      DatabaseCleaner.clean
    end
  end  
end

The problem seems to have something to do that the capybara web server uses its own database connections (as opposed to the connection used by the tests itself), but the in memory database is only available for the one connection which created it.

This question already provided some insights: (DatabaseError: no such table: django_session) ERROR during Django 1.3 selenium testing

So, how can I make selenium tests memory db compatible?

Thank you very much in advance.

Community
  • 1
  • 1

1 Answers1

1

The capybara Readme itself suggests to monkeypatch the ActiveRecord::Basein the spec_helper.rb

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

If you use spork, the last line belongs to the Spork.each_run section. In this case, you also have to load the schema.rb in Spork.each_run.

This actually works but is advised to use with caution. Further information see Why not use shared ActiveRecord connections for Rspec + Selenium?

Community
  • 1
  • 1