如何使用 Factory Girl 定义多个关联对象?

发布于 2024-07-25 02:02:15 字数 848 浏览 11 评论 0原文

Factory Girl 文档提供了这种语法来创建(我猜)父子关联......

  Factory.define :post do |p|
    p.author {|a| a.association(:user) }
  end

帖子属于用户(其“作者”)。

如果您想定义一个工厂来创建具有一堆 PostUser,该怎么办?

或者如果是多对多的情况怎么办(例如,请参阅下面的更新)?


更新

我以为我已经弄清楚了。 我尝试了这个...

Factory.define(:user) do |f|
  f.username { Factory.next(:username) }

  # ...

  f.roles { |user|
    [
      Factory(:role),
      Factory(:role, {:name => 'EDIT_STAFF_DATA'})
    ]
  }
end

一开始似乎有效,但后来我收到验证错误,因为 FG 试图使用相同的用户名和电子邮件保存用户两次。

所以我回到原来的问题。 如果你有一个多对多的关系,比如UsersRoles,你如何定义一个工厂来返回Users和一些相关的角色? 请注意,Roles 必须是唯一的,因此我不能让 FG 每次创建 User 时都在数据库中创建新的“ADMIN”Role >。

The Factory Girl docs offer this syntax for creating (I guess) parent-child associations...

  Factory.define :post do |p|
    p.author {|a| a.association(:user) }
  end

A post belongs to a User (its "author").

What if you want to define a Factory to create Users, that have a bunch of Posts?

Or what if it's a many-to-many situation (see update below for example)?


UPDATE

I thought I had figured it out. I tried this...

Factory.define(:user) do |f|
  f.username { Factory.next(:username) }

  # ...

  f.roles { |user|
    [
      Factory(:role),
      Factory(:role, {:name => 'EDIT_STAFF_DATA'})
    ]
  }
end

It seemed to work at first, but then I got validation errors because F.G. was trying to save the user twice with same username and email.

So I return to my original question. If you have a many-to-many relationship, like say Users and Roles, how can you define a Factory that will return Users with some associated Roles? Note that Roles must be unique, so I can't have F.G. creating a new "ADMIN" Role in the DB every time it creates a User.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就是爱搞怪 2024-08-01 02:02:15

我不确定这是否是最正确的方法,但它确实有效。

Factory.define(:user) do |u|
  u.login 'my_login'
  u.password 'test'
  u.password_confirmation 'test'
  u.roles {|user| [user.association(:admin_role),
                      user.association(:owner_role, :authorizable_type => 'User', :authorizable_id => u.id) ]}
end

I'm not sure if this is the most correct way of doing it, but it works.

Factory.define(:user) do |u|
  u.login 'my_login'
  u.password 'test'
  u.password_confirmation 'test'
  u.roles {|user| [user.association(:admin_role),
                      user.association(:owner_role, :authorizable_type => 'User', :authorizable_id => u.id) ]}
end
初见终念 2024-08-01 02:02:15

最近的对工厂女孩的更新允许关联用回调块指定

A recent update to factory girl allows associations to be specified with callback blocks

晨与橙与城 2024-08-01 02:02:15

我创建了一个 active_factory 插件,它可以处理您在规范中的情况,如下所示:

models { user - posts(3) }

如果有兴趣,我可以尝试与factory_girl工厂创建集成。

I created an active_factory plugin, that deals with your situation in spec like this:

models { user - posts(3) }

If there is an interest, i may try to create integration with factory_girl factories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文