0

我正在使用内存 sqlite 数据库来运行 rspec 测试。这功能非常好。只有在运行 selenium 驱动测试 ( describe "does something", :js => true do) 时,启动 webbrowser 才会收到错误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 ,我正在寻找一种解决方案,以便在使用内存数据库时运行 selenium 驱动测试。

细节:

我在 Rails 4.0 和以下 gem 上使用 ruby​​(摘录)

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

数据库.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

问题似乎与 capybara web 服务器使用自己的数据库连接(与测试本身使用的连接相反)有关,但内存数据库仅可用于创建它的一个连接。

这个问题已经提供了一些见解: (DatabaseError: no such table: django_session) ERROR during Django 1.3 selenium testing

那么,如何使硒测试内存数据库兼容?

非常感谢您提前。

4

1 回答 1

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?

于 2013-10-22T08:21:41.690 回答