Spork、RSpec 和 database_cleaner 破坏开发数据库

发布于 2024-12-07 19:52:21 字数 1467 浏览 1 评论 0原文

我的 Rails 3.1 应用程序中有以下 spec_helper.rb 文件。我正在使用 Spork 更快地加载环境以进行测试。在将 Spork 添加到混合物中之前,我的所有测试都有效。添加 spork 后,测试数据库在测试运行之间没有得到正确的清除,这超出了我的一些期望。

按照其他说明,我将 database_cleaner 添加到下面列出的代码中;然而,现在,开发数据库和测试数据库正在被清理。 ENV["RAILS_ENV"] 调用在此调用期间返回测试。

有没有办法显式限制对 DatabaseCleaner.clean_with(:truncation) 的调用仅影响测试数据库?

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'shoulda/matchers/integrations/rspec'
  require 'database_cleaner'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :mocha

    config.formatter = 'documentation'
    config.use_transactional_fixtures = true

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

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

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

Spork.each_run do
  FactoryGirl.reload
end

更新:这是我的database.yml 文件

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

另外,我通过将 clean_with 调用移动到 before(:each) 部分来解决基本问题,但这会显着减慢测试运行速度。

I have the following spec_helper.rb file in my Rails 3.1 application. I am using Spork to load the environment faster for testing. All my tests worked prior to adding Spork to the mix. After adding spork, the test database was not getting properly cleared between test runs which threw off some of my expectations.

Following other instructions, I added database_cleaner to the mix with the code listed below; however, now, the development database is getting cleaned up as well as the test database. The ENV["RAILS_ENV"] call is returning test during this call.

Is there a way to explicitly limit the call for DatabaseCleaner.clean_with(:truncation) to only affect the test database?

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'shoulda/matchers/integrations/rspec'
  require 'database_cleaner'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :mocha

    config.formatter = 'documentation'
    config.use_transactional_fixtures = true

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

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

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

Spork.each_run do
  FactoryGirl.reload
end

Update: Here is my database.yml file

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

Also, I have worked around the basic problem by moving the clean_with call into the before(:each) section, but this slows down the test runs significantly.

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

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

发布评论

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

评论(2

总以为 2024-12-14 19:52:21

您是否尝试将 ENV["RAILS_ENV"] ||= 'test' 移出 Spork.prefork 块?

你确定 Spork 导致你的数据库没有被清理吗?如果您使用 RSpec 的事务装置,唯一可能导致这种情况的事情是在 before(:all) 块中使用工厂。您可以清理 after(:all) 块中的数据并摆脱 DatabaseCleaner。

顺便说一句,如果您使用截断策略,则无需运行 DatabaseCleaner.start

Have you tried to move ENV["RAILS_ENV"] ||= 'test' out of the Spork.prefork block?

Are you sure that Spork caused your DB got uncleaned? If you're using RSpec's transactional fixtures, the only thing that could lead to such thing is using factories within before(:all) block. You can clean up data in after(:all) block and get rid of DatabaseCleaner.

BTW, if you're using truncation strategy, there's no need to run DatabaseCleaner.start.

舂唻埖巳落 2024-12-14 19:52:21

您的 RAILS_ENV 是否明确设置为“开发”?如果是这样,默认的spec_helper将针对开发数据库运行测试。如果您从 vim 内部运行测试,或在命令行上运行 rspec,那么它将针对您的开发数据库运行测试。

当我被迫使用database_cleaner时,我喜欢显式设置RAILS_ENV以在spec_helper中进行测试以保护我的开发数据库。每当您使用事务性设备以外的其他东西时,这可能是个好主意。

我注意到您同时使用了事务装置和database_cleaner。如果您的数据库支持事务,则根本不需要database_cleaner。如果您正在使用 Mongo,或者您正在运行 capybara-webkit 测试,那么您将需要 database_cleaner,并且需要禁用事务装置。

Is your RAILS_ENV explicitly set to "development"? If so, the default spec_helper will run the tests against the development DB. If you run a test from inside vim, or run rspec on the command-line, then it'll run the test against your development DB.

When I'm forced to use database_cleaner, I like to explicitly set RAILS_ENV to test in spec_helper to protect my development DB. Any time you're using something other than transactional fixtures, this is probably a good idea.

I note that you're using both transactional fixtures and database_cleaner. You don't need database_cleaner at all if your DB supports transactions. If you're using Mongo, or you are running capybara-webkit tests, then you'll need database_cleaner, and will want to disable transactional fixtures.

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