Rails 测试数据库不会擦除

发布于 2024-10-15 12:46:39 字数 191 浏览 1 评论 0原文

我正在运行 Rails 3.0.3 并将 rspec-rails 2.4.1 与 postgresql 数据库一起使用。每当我运行 RSpec 测试时,数据都会保留在最后。有谁知道如何让 Rails 或 rspec 在每次使用之间擦除测试环境的数据?

请告诉我是否有任何进一步的信息可以让我更轻松地回答我的问题。

谢谢!
特里斯坦

I'm running rails 3.0.3 and using rspec-rails 2.4.1 with a postgresql database. Whenever I run my RSpec tests, the data remains at the end. Does anyone know how to get rails or rspec to wipe the test environment's data between each use?

Please tell me if there's any further information that could make answering my question easier.

Thanks!
Tristan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

作妖 2024-10-22 12:46:39

安装database_cleaner gem,然后将其添加到您的spec_helper.rb中。

Spec::Runner.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

Install the database_cleaner gem and then add this to your spec_helper.rb.

Spec::Runner.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end
静若繁花 2024-10-22 12:46:39

使用事务示例在每次测试运行后回滚数据

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

Use transactional examples to rollback the data after every test run

RSpec.configure do |config|
  config.use_transactional_examples = true
end
讽刺将军 2024-10-22 12:46:39

您不需要任何额外的 gem 来在运行之间清理测试数据库。在您的spec_helper.rb 文件中,按如下方式配置rspec:

RSpec.configure do |c|
  c.around(:each) do |example|
    ActiveRecord::Base.connection.transaction do
      example.run
      raise ActiveRecord::Rollback
    end
  end
end

You don't need any extra gem in order to clean your test DB between runs. In your spec_helper.rb file, configure rspec as follows:

RSpec.configure do |c|
  c.around(:each) do |example|
    ActiveRecord::Base.connection.transaction do
      example.run
      raise ActiveRecord::Rollback
    end
  end
end
甜柠檬 2024-10-22 12:46:39

我刚刚经历过的另一种可能性是使用了错误的 before 块。

我不小心将 before 块设置为 all 而不是 each

before :all do
  user = FactoryGirl.create(:user)
  sign_in user
end

这导致 user 停留在整个 rspec 运行的数据库,这导致了验证冲突。

相反,before 应该是 each,以便通过 rspec 运行,一切都保持干净:

before :each do
  user = FactoryGirl.create(:user)
  sign_in user
end

如果您犯了这个错误,那么您在一切恢复正常之前,可能需要手动清理测试数据库。最简单的方法可能是截断每个表(除了 schema_migrations 之外)。

Another possibility, that I just put myself through, is using the wrong before block.

I accidentally set a before block as an all instead of an each:

before :all do
  user = FactoryGirl.create(:user)
  sign_in user
end

This caused the user to stick around in the database for the entire rspec run, which caused validation collisions.

Instead, the before should be an each so that everything is kept clean through the rspec run:

before :each do
  user = FactoryGirl.create(:user)
  sign_in user
end

If you've made this mistake, then you will probably need to manually clean up your test database before things go back to normal. The simplest way to do that is probably to truncate each of the tables (aside from schema_migrations).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文