2

我对 TDD 很陌生,并选择使用上述 Gems。我认为我已经正确设置它,因为我可以运行我的测试。但是,我无法弄清楚如何从 db/seeds.rb 填充我的测试数据库。当我调用

rake db:seed RAILS_ENV=test

在终端中,我可以通过 PGAdmin 看到在数据库中创建的行。但是,当我使用以下内容运行测试时

rake minitest:all

之后数据库最终变为空白,并且在我保存屏幕截图的测试中,数据库中的项目不会像我在开发时那样出现在前端。

我的 test_helper.rb 包含以下内容。

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/rails/capybara'
require 'minitest/focus'
require 'minitest/colorize'
Capybara.javascript_driver = :webkit

class ActiveSupport::TestCase
  fixtures :all
    DatabaseCleaner.strategy = :transaction

    class MiniTest::Spec
      before :each do
            Rake::Task["db:seed"].invoke
            DatabaseCleaner.start
      end

      after :each do
            DatabaseCleaner.clean
      end
    end
end

对于一些额外的背景,我的 db/seeds.rb 文件(当使用 rake 手动播种时有效)

ProgramIndustry.delete_all
ProgramIndustry.create([
        { name: 'Accounting and finance'},
        { name: 'Banking'},
        { name: 'Construction'},
        { name: 'Education'}
])

为什么测试开始时数据库不会填充种子.rb?

4

1 回答 1

5

您的数据库是空白的,因为您使用的是 DatabaseCleaner,它会从数据库中删除数据。我假设这就是您希望 test_helper.rb 文件的样子:

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/rails/capybara'
require 'minitest/focus'
require 'minitest/colorize'

Capybara.javascript_driver = :webkit

class ActiveSupport::TestCase
  fixtures :all

  DatabaseCleaner.strategy = :transaction

  before do
    DatabaseCleaner.start
    Rake::Task["db:seed"].invoke # seed after starting
  end

  after do
    DatabaseCleaner.clean
  end
end

我不知道从 before 钩子中调用 db:seed 任务,这似乎有点可疑。但是我不使用 DatabaseCleaner,因为我更喜欢使用 Fixtures 和 ActiveSupport::TestCase 支持的事务。

我不知道您为什么要使用 DatabaseCleaner,但是当您在 Minitest 中使用 RSpec 语法时,我假设您只是在尝试直到它们起作用。我是否可以建议删除 DatabaseCleaner 并将所有测试数据放在夹具中,并使用以下内容来管理跨线程的数据库事务:

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/rails/capybara'
require 'minitest/focus'
require 'minitest/colorize'

class ActiveSupport::TestCase
  fixtures :all
end

# Capybara driver
Capybara.javascript_driver = :webkit

# Make all database transactions use the same thread
ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
  def current_connection_id
    Thread.main.object_id
  end
end

如果您对此有疑问,请考虑以下变化:

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/rails/capybara'
require 'minitest/focus'
require 'minitest/colorize'

class ActiveSupport::TestCase
  fixtures :all
end

# Capybara driver
Capybara.javascript_driver = :webkit

# Make all database transactions use the same thread
class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
于 2013-10-02T20:45:10.860 回答