0

我有包含一些设置数据的工厂。例如:

Factory.define :event do |event|
  event.name  { Factory.next(:email) }
  event.blurb "Test event blurb"
  event.association(:owner, :factory => :user)
  event.countries Country.all
end

Country.all 只是将查找表中的所有国家/地区分配给该特定事件。在我的测试助手中使用这条线运行测试之前,我通过加载种子来包括所有国家/地区:

require "#{Rails.root}/db/seeds.rb"

这在运行单个单元测试时效果很好:

ruby test/unit/event_test.rb

但是,当我使用以下方法运行测试时,Country.all 什么也不返回:

rake test:units

有谁知道为什么会这样?

4

2 回答 2

1

您需要 test_helper 中的种子,它已加载一次。每次测试运行后,数据库都会被清除,包括种子数据。为了让种子每次都加载,在你的 test_helper 的ActiveSupport::TestCase类定义中添加这样的内容。

class ActiveSupport::TestCase
  # this line:
  setup { load "#{Rails.root}/db/seeds" }
end
于 2011-01-11T06:16:07.717 回答
0

查看rakegem 的源代码。看起来您必须seeds.rb在每个测试文件中手动加载文件,或者更好的是,从test_helper.rb.

于 2011-01-11T05:17:41.470 回答