使用Minitest-rails,Capybara,Capybara-webkit和Rails 4测试数据库播种

时间:2013-10-02 19:45:05

标签: ruby-on-rails capybara minitest

我对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'}
])

为什么在测试开始时数据库不会用seeds.rb填充?

1 个答案:

答案 0 :(得分: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并将所有测试数据放入fixture中并使用以下命令管理跨线程的数据库事务:

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