Rspec 集成测试不清理数据库

发布于 2024-10-31 09:20:02 字数 70 浏览 1 评论 0原文

每次集成测试后数据库都不会被清理。该值保留在数据库中。

我应该有一个选择来实现这一点吗?

谢谢

The database is not being cleaned after each integration test. The value stays in the database.

Is there an option I should have to make this happen?

Thanks

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

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

发布评论

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

评论(6

べ映画 2024-11-07 09:20:02

对于任何使用 before(:all) 挂钩的人,请注意这些挂钩是在打开与固定装置关联的事务之前执行的。这意味着由 before(:all) 挂钩创建的任何数据都不会被事务固定装置回滚。您可以RSpec 文档阅读更多内容。

我只是想提一下这一点,因为我被它咬住了,我最初的本能是跳到数据库清理器(最终不需要它并最终不起作用)。

For anyone using before(:all) hooks, be aware that these hooks are executed before the transaction associated to the fixture is opened. This means that any data created by before(:all) hooks will not be rolled back by transactional fixtures. You can read more in the RSpec documentation.

I just wanted to mention this because I was bit by it and my initial instinct was to jump to Database Cleaner (which wound up not being needed and eventually not working).

李不 2024-11-07 09:20:02

如何在不运行 rake spec 的情况下为 Rails rspec 测试准备测试数据库?

您可能会对我的答案感兴趣。这是一个很好的解决方案。对于您的情况,您可能需要类似的东西

config.after :each do
  ActiveRecord::Base.subclasses.each(&:delete_all)
end

How do I prepare test database(s) for Rails rspec tests without running rake spec?

My answer there might be of interest to you. it's a nice solution. For your case, you would probably need something like

config.after :each do
  ActiveRecord::Base.subclasses.each(&:delete_all)
end
月竹挽风 2024-11-07 09:20:02

在这里查看教程:http://railscasts.com/episodes/257-request -specs-and-capybara

它描述了除 Rspec 和 Capybara 之外的数据库清理器

Look here for a tutorial: http://railscasts.com/episodes/257-request-specs-and-capybara

It describes Database Cleaner besides Rspec and Capybara

香草可樂 2024-11-07 09:20:02

您需要 DatabaseCleaner,但您可能会发现 :truncation 策略有点太慢,无法始终运行。它实际上只适用于集成测试,因此您可以这样做:

# spec/spec_helper.rb
require 'database_cleaner'
config.before(:suite) do
  DatabaseCleaner.clean_with :truncation
  DatabaseCleaner.strategy = :transaction
end

config.before(:each) do |group|
  # The strategy needs to be set before we call DatabaseCleaner.start
  case group.example.metadata[:type]
  when :feature
    DatabaseCleaner.strategy = :truncation
  else
    DatabaseCleaner.strategy = :transaction
  end
  DatabaseCleaner.start
end

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

# spec/features/your_feature_spec.rb
require 'spec_helper'
describe "An integration test", :type => :feature do
end

# spec/model/your_model_spec.rb
require 'spec_helper'
describe "A unit test" do
end

显然,这只适用于直接使用 RSpec 进行集成测试而不是使用 Cucumber 进行集成测试。

You want DatabaseCleaner, but you may find that the :truncation strategy is a bit too slow to run all the time. It's really only necessary for integration tests, so you can do this:

# spec/spec_helper.rb
require 'database_cleaner'
config.before(:suite) do
  DatabaseCleaner.clean_with :truncation
  DatabaseCleaner.strategy = :transaction
end

config.before(:each) do |group|
  # The strategy needs to be set before we call DatabaseCleaner.start
  case group.example.metadata[:type]
  when :feature
    DatabaseCleaner.strategy = :truncation
  else
    DatabaseCleaner.strategy = :transaction
  end
  DatabaseCleaner.start
end

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

# spec/features/your_feature_spec.rb
require 'spec_helper'
describe "An integration test", :type => :feature do
end

# spec/model/your_model_spec.rb
require 'spec_helper'
describe "A unit test" do
end

Obviously, this only applies if you're doing integration tests with RSpec directly vs. doing them with Cucumber.

酒中人 2024-11-07 09:20:02

有两种方法可以实现此目的:

  1. 为每个单独的测试配置事务示例。
  2. 为所有测试配置事务示例。

如果您选择选项 1:在规范文件的顶部,之后:

require 'spec_helper'

添加:

RSpec.configure {|c| c.use_transactional_examples = true }

这将在每个示例之后回滚事务。

2.如果要全局配置,那么,在spec_helper.rb中

RSpec.configure do |config|
...
config.use_transactional_examples = true # Add this
...
end

There are two ways to accomplish this:

  1. Configure transactional examples for each individual test.
  2. Configure transactional examples for all the tests.

If you opt for option 1: At the top of the spec file, after:

require 'spec_helper'

Add:

RSpec.configure {|c| c.use_transactional_examples = true }

That will roll back the transactions after each example.

2.If you want to configure it globally, then, in the spec_helper.rb

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