Rails 3和Rspec 2关闭各个测试的事务夹具

时间:2010-10-11 15:52:48

标签: ruby-on-rails rspec rspec2

我正在将我的应用程序升级到Rails 3.我开始在Rails 3中使用Rspec 2.我需要为我的一些rspec测试关闭事务处理程序。之前我在我的模型规范中使用了以下代码

 before(:all) do
    ActiveSupport::TestCase.use_transactional_fixtures = false
  end

  after(:all) do
    ActiveSupport::TestCase.use_transactional_fixtures = true
    clean_engine_database
  end

现在给了我错误:

 Failure/Error: ActiveSupport::TestCase.use_transactional_fixtures = false
     undefined method `use_transactional_fixtures=' for ActiveSupport::TestCase:Class

有没有办法在Rails 3中使用Rspec 2的每个测试块执行此操作?

3 个答案:

答案 0 :(得分:19)

我正在寻找这个问题的答案,遇到this blog entry

它建议在describe块中声明

describe "xxx" do
  self.use_transactional_fixtures = false
  ...

我用Rails 3.0.7和RSpec 2.6.3一起尝试过,看起来像是在工作。

答案 1 :(得分:0)

您可以通过将config.use_transactional_fixtures = false放在spec_helper.rb上来全局禁用事务处理程序。如果您想通过测试来控制它们(例如,只在其中一些上使用事务),您可以使用DatabaseCleaner设置此行为。

我在浏览器上使用javascript测试页面时遇到了相关问题(这种情况不适用于事务性灯具)。以下是我设法解决这个问题的方法:http://github.com/lailsonbm/contact_manager_app

答案 2 :(得分:0)

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end