为什么不为 Rspec + Selenium 使用共享的 ActiveRecord 连接? [英] Why not use shared ActiveRecord connections for Rspec + Selenium?
问题描述
似乎最普遍接受的处理 Selenium 和测试的方法是避免使用事务性装置,然后在测试/场景之间使用诸如 database_cleaner 之类的东西.我最近遇到了以下 文章 建议执行以下操作:
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/scenarios. I recently ran into the following article which suggested doing the following:
spec_helper.rb
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.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
这似乎比替代方案的性能更好.有没有人有任何理由不应该使用它?
This seems loads better for performance than the alternatives. Does anyone have any reason why this shouldn't be used?
推荐答案
该解决方案由 Jose Valim 编写 - 在 Rails 社区中备受推崇,并且是 Rails 核心团队的成员.如果有问题,我怀疑他会推荐使用它.我个人没有任何问题.
This solution was written by Jose Valim - well respected in the Rails community and a member of the Rails core team. I doubt he would recommend using it if there were issues with it. I personally haven't had any issues.
请注意,如果您使用 Spork,这需要在 each_run 块中才能工作.
Just be aware that if you use Spork this needs to be in the each_run block to work.
FWIW - 我在 Postgres 上的上述补丁中遇到了间歇性的水豚测试问题.@hsgubert 下面的 Mike Perham 解决方案似乎已经解决了这些问题.我现在正在使用该解决方案.
FWIW - I have had intermittent capybara test issues with the above patch on Postgres. The Mike Perham solution that @hsgubert has below appears to have solved those issues. I am now use that solution.
这篇关于为什么不为 Rspec + Selenium 使用共享的 ActiveRecord 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!