简体   繁体   English

FactoryGirl不保存记录

[英]FactoryGirl Does Not Save Records

This is a follow on to this question: Factory_Girl not generating unique records 这是该问题的后续内容: Factory_Girl不生成唯一记录

The reason that requirements records are not being created is because the user records are not being created properly (or at least the associations). 未创建需求记录的原因是因为未正确创建用户记录(或至少未创建关联)。

Given the following factories: 鉴于以下工厂:

factory :user do
  sequence(:name) { |n| "foo#{n}" }
  password "fooBar_1"
  email { "#{name}@example.com" }
  app_setting
  factory :adminUser do |user|
    user.role_assignments { |ra| [ra.association(:adminAssignment)] }
  end
  factory :reader do |user|
    user.role_assignments { |ra| [ra.association(:readerAssignment)] }
  end
end

factory :role_assignment do
  factory :adminAssignment do |ra|
    ra.association :role, factory: :adminRole
  end
  factory :readerAssignment do
    association :role
  end
end

factory :role do
  roleName "reader"
  factory :adminRole do |r|
    r.roleName "Administrator"
  end
end

The following snippet of code, should yield a user with a related role_assignment record with a related role record with a roleName of "Administrator" 以下代码段应产生一个具有相关role_assignment记录和一个RoleName为“ Administrator”的相关角色记录的用户

user = FactoryGirl.create(:adminUser)

This will actually work in the rails console - records are created in the database as expected. 这实际上将在rails控制台中工作-记录将按预期在数据库中创建。 However, when the line of code is executed during setup, the records aren't saved, and tests that rely on the role assignment fail. 但是,在设置过程中执行代码行时,不会保存记录,并且依赖角色分配的测试也会失败。

Any suggestions on where to look to figure out why the records are not being committed? 关于在何处查找为什么未提交记录的任何建议?

It turns out that the factory wasn't working quite so well in the Rails console. 事实证明,工厂在Rails控制台中的运行状况不是很好。 Although records were being created in all the right tables, the role_assignment record had nil for the user id. 尽管在所有正确的表中都创建了记录,但role_assignment记录的用户ID为零。 Apparently the format I was using to deal with the related many was not correct. 显然我用来处理相关内容的格式不正确。

The correct format comes from https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md , and is as follows (for my circumstance): 正确的格式来自https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md ,如下所示(根据我的情况):

factory :user do
sequence(:name) { |n| "foo#{n}" }
    password "fooBar_1"
    email { "#{name}@example.com" }
    app_setting
 factory :adminUser do
   after(:create) do |user, evaluator|
     FactoryGirl.create_list(:adminAssignment, 1, user: user)
   end
 end

I still need to clean up the other portions of the user factory, so I haven't included them here. 我仍然需要清理用户工厂的其他部分,因此这里没有包括它们。 This creates the user, then creates the role and role_assignment which then link properly. 这将创建用户,然后创建角色和role_assignment,然后正确链接。

My other problem actually stemmed from the fact that I am doing a major remodel of the application and hadn't looked back at the scope definition in the requirements model before writing the factory, so the records I was creating would never be in scope. 我的另一个问题实际上源于以下事实:我正在对应用程序进行重大重构,并且在编写工厂之前没有回头查看需求模型中的范围定义,因此我创建的记录将永远不在范围内。 Oops. 哎呀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM