Ruby on rails Rails 4:使用sqlite内存数据库进行selenium驱动的测试
我正在使用内存sqlite数据库来运行rspec测试。这个功能非常好。只有在运行selenium驱动的测试(Ruby on rails Rails 4:使用sqlite内存数据库进行selenium驱动的测试,ruby-on-rails,sqlite,selenium,Ruby On Rails,Sqlite,Selenium,我正在使用内存sqlite数据库来运行rspec测试。这个功能非常好。只有在运行selenium驱动的测试(descripe“does”,:js=>true do)时,启动的webbrowser才会收到错误SQLite3::SQLException:没有这样的表:用户:从“users”中选择“users”。*。。。 WEBrick/1.3.1(Ruby/2.0.0/2013-02-24)127.0.0.1:57827 我正在寻找一种解决方案,在使用内存数据库时运行selenium驱动的测试 详情
descripe“does”,:js=>true do
)时,启动的webbrowser才会收到错误SQLite3::SQLException:没有这样的表:用户:从“users”中选择“users”。*。。。
WEBrick/1.3.1(Ruby/2.0.0/2013-02-24)127.0.0.1:57827
我正在寻找一种解决方案,在使用内存数据库时运行selenium驱动的测试
详情:
我使用RubyonRails4.0和以下gems(摘录)
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
问题似乎与capybara web服务器使用自己的数据库连接有关(与测试本身使用的连接相反),但内存中的数据库仅可用于创建它的一个连接
这个问题已经提供了一些见解:
那么,如何使selenium测试与内存db兼容
非常感谢您。本身建议monkeypatch在spec\u 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
如果使用spork,最后一行属于spork.each_run
部分。
在这种情况下,还必须在Spork.each_运行
中加载schema.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