Rails 带有命名空间的固定装置的迷你测试

Rails minitest with namespaced fixtures

我有一个命名空间模型,即Billing::Plan。所以我把它的夹具放在 test/fixtures/test/billing/plan.yml 下。 (实际上,是 rails 生成器把它放在那里,所以我认为这是一个 配置的约定 愉快:-))

现在,当我 运行 一个测试它工作时,但是当我尝试 运行 我的所有测试套件 rake testguard 夹具加载失败有这个错误

ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "plans" does not exist

重要的部分是

ERROR:  relation "plans" does not exist

我的 test_helper.rb 文件中的 fixtures :all 命令似乎无法理解关系名称是 billing_plans 而不是 plans

这是为什么?

我看 fixtures :all does.

就明白了

需要调用 set_fixture_class 才能将 table 名称正确分配给命名空间装置。

所以我解决了我的问题添加...

# test/test_helper.rb

module ActiveSupport
  class TestCase
    fixtures :all
    set_fixture_class 'billing/plan' => Billing::Plan # <= ...this line!
 end
end