Minitest 中的夹具错误:table 没有名为 X 的列

Fixture error in Minitest: table has no column named X

我在尝试为我的 user_tests 模型设置测试时碰壁了。

我在 运行 测试 (minitest) 时得到的错误(许多例子中的一个):

ERROR["test_should_get_new", Admin::TestConfigsControllerTest, 2016-01-07 13:04:02 +0100]
test_should_get_new#Admin::TestConfigsControllerTest (1452168242.12s)
ActiveRecord::Fixture::FixtureError:
     ActiveRecord::Fixture::FixtureError: table "user_tests" has no column named "user".

没有名为 user 的列是正确的,但是有一个 user_id 并且在其他设备中,它工作正常。我的设置有什么问题,找不到此模型的列?

夹具 user_tests.yml:

user_test_one:
  started: false
  started_at:
  finished: false
  finished_at:
  invited: true
  invited_at: <%= Time.zone.now %>
  user: michael
  test_config: test_config_one
  test_result:
  test_result_type:

user_test_two:
  started: false
  started_at:
  finished: false
  finished_at:
  test_result:
  test_result_type:
  invited: false
  invited_at:
  user: michael
  test_config: test_config_two
  test_result:
  test_result_type:

此处 usertest_configtest_result 都应指向列 user_idtest_config_idtest_result_id。都给出相同类型的错误。

它适用于 users.yml,其中公司和 user_group 都是指向其他表的链接(向 Michael Hartle 大声疾呼 Ruby 在 Rails 教程中的出色表现) :

michael:
  first_name: Michael
  last_name: Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>
  company: my-company
  user_group: my-company-groep

archer:
  first_name: Sterling
  last_name: Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>
  company: my-company
  user_group: my-company-groep

users_test.rb 模型的第一行:

class UserTest < ActiveRecord::Base
  belongs_to :user, required: true
  belongs_to :test_config, required: true
  belongs_to :test_result, polymorphic: true
  attr_accessor :step, :send_invitation

也找不到数据库有什么问题。这是 SQLite 中 usersuser_tests 表的屏幕截图:

有什么想法吗?

Update: 一定是测试没有找到这个fixture(user_tests.yml)的型号user_test.rb。我可以将列 usertest_config 转换为包括 _id 但随后发生错误,即 created_atupdated_at 列可能不是零(ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: user_tests.created_at).这些应该由 Rails 处理,但不是。为什么?

user_test_one:
  started: false
  started_at:
  finished: false
  finished_at:
  invited: true
  invited_at: <%= Time.zone.now %>
  test_config_id: 1
  user_id: 1
  created_at: <%= Time.zone.now %>
  updated_at: <%= Time.zone.now %>

更新 2:我强烈怀疑问题是由 model/table 名称中包含 "Test" 引起的

将 table 和所有相应代码从 user_test(和所有变体)重命名为 user_survey 解决了问题。