为什么不对Rspec + Selenium使用共享的ActiveRecord连接? [英] Why not use shared ActiveRecord connections for Rspec + Selenium?

查看:66
本文介绍了为什么不对Rspec + Selenium使用共享的ActiveRecord连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎最普遍接受的处理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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆