【问题标题】:Neither rspec or database_cleaner will roll back my testsrspec 或 database_cleaner 都不会回滚我的测试
【发布时间】:2015-08-11 01:56:17
【问题描述】:

由于我无法弄清楚的原因,每次测试后我的数据库都没有得到清理。 我是靠 config.use_transactional_fixtures = true

但这不起作用,所以我使用了 database_cleaner,但这也不起作用。

我已经根据自述文件配置了数据库清理器:

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.strategy = :deletion
    DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end

但是这个测试失败了:

describe "db_cleaner" do
    it "should clean db" do
      FactoryGirl.create :question
      expect(Question.all.count).to eq(1)
    end
end

Failure/Error: expect(Question.all.count).to eq(1)
expected: 1
got: 7

(并且 got 每次都继续递增)

我正在调用测试

bin/rspec spec/models/concerns/results_compiler_spec.rb:12

我在跑步

rspec-rails 2.99.0
rails 4.2.0
factory_girl_rails 4.5.0
database_cleaner 1.4.1

非常感谢任何帮助。

【问题讨论】:

    标签: ruby-on-rails-4 rspec


    【解决方案1】:

    我从来没有在这个特定的项目中找到答案,但我在最近的一个项目中遇到了它,发现这是因为我忘记包含了

    require 'rails_helper'
    

    在我的规范中。

    (带有它的规格可以很好地清洁,没有的规格则不是)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多