如何在 Factory Girl 中创建与 has_many :through 关系的关联?

发布于 2024-12-22 09:06:21 字数 953 浏览 0 评论 0原文

在我的模型中,我有以下设置:

class User < ActiveRecord::Base
  has_many :assignments
  has_many :roles, :through => :assignments
end


class Role < ActiveRecord::Base
  has_many :assignments
  has_many :users,  :through => :assignments
end

class Assignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :role

  attr_accessible :role_id, :user_id
end

在我的factory.rb 文件中,我有:

FactoryGirl.define do
  factory :user do
    sequence(:username) { |n| "user#{n}" }
    email { "#{username}@example.com" }
    password 'secret'
    password_confirmation 'secret'

    factory :admin  do
      ...
    end
  end

  factory :role do
    name 'Normal'
    value 'normal'
  end

  factory :assignment do
    ...
  end
end

我正在努力弄清楚如何添加角色, :name => “管理员”,:值=> “admin”,到“user”块内的“admin”工厂,这样我就可以调用

create(:admin)

我的测试并让用户具有管理员角色。

谢谢您的关注。

In my models I have the following setup:

class User < ActiveRecord::Base
  has_many :assignments
  has_many :roles, :through => :assignments
end


class Role < ActiveRecord::Base
  has_many :assignments
  has_many :users,  :through => :assignments
end

class Assignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :role

  attr_accessible :role_id, :user_id
end

In my factory.rb file I have:

FactoryGirl.define do
  factory :user do
    sequence(:username) { |n| "user#{n}" }
    email { "#{username}@example.com" }
    password 'secret'
    password_confirmation 'secret'

    factory :admin  do
      ...
    end
  end

  factory :role do
    name 'Normal'
    value 'normal'
  end

  factory :assignment do
    ...
  end
end

I'm struggling to figure out how I would add a role with, :name => "admin", :value => "admin", to the "admin" factory inside the "user" block so I can call

create(:admin)

in my tests and have a user with the admin role.

Thank you for looking.

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

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

发布评论

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

评论(2

_畞蕅 2024-12-29 09:06:21

对于这样的工厂,您需要使用工厂女孩的回调。
试试这个:

FactoryGirl.define do
  factory :user do
    ...
  end

  factory :admin, :parent => :user do 
    after_create {|u| Factory(:assignment, :role => Factory(:role, :name => 'admin', :value => 'admin'), :user => u)}
  end

  factory :role do
    ...
  end

  factory :assignment do
    user {|a| a.association(:user)}
    role {|a| a.association(:role)}
  end
end

For such a factory you need to use callbacks of factory girl.
Try this:

FactoryGirl.define do
  factory :user do
    ...
  end

  factory :admin, :parent => :user do 
    after_create {|u| Factory(:assignment, :role => Factory(:role, :name => 'admin', :value => 'admin'), :user => u)}
  end

  factory :role do
    ...
  end

  factory :assignment do
    user {|a| a.association(:user)}
    role {|a| a.association(:role)}
  end
end
于我来说 2024-12-29 09:06:21

@kshil 是正确的,但您可以稍微收紧代码并使其更加模块化。

为管理员用户创建第二个 :role 工厂。

factory :role do
  name 'Normal'
  value 'normal'

  factory :admin_role do
    name  'admin'
    value  'admin'
  end
end

此外,如果工厂名称与关联名称相同,则可以省略工厂名称。 :assignment 工厂变为:

factory :assignment do
  user
  role
end

:user 工厂内定义 :admin_user 工厂,并且不必指定父工厂。您还可能添加两个工厂来定义普通用户和管理员用户。

factory :user do
  sequence(:username) { |n| "user#{n}" }
  email { "#{username}@example.com" }
  password 'secret'
  password_confirmation 'secret'

  factory :normal_user do
    after_create {|u| Factory(:assignment, :user => u)}
  end

  factory :admin_user do
    after_create {|u| Factory(:assignment, :role => Factory(:admin_role), :user => u)}
  end
end

@kshil is correct but you can tighten up the code a little and make it more modular.

Create a second :role factory for the admin user.

factory :role do
  name 'Normal'
  value 'normal'

  factory :admin_role do
    name  'admin'
    value  'admin'
  end
end

Also, if a factory name is the same as the association name you can leave out the factory name. The :assignment factory becomes:

factory :assignment do
  user
  role
end

Define the :admin_user factory inside the :user factory and you don't have to specify the parent factory. You would also probably to add two factories to define both normal and admin users.

factory :user do
  sequence(:username) { |n| "user#{n}" }
  email { "#{username}@example.com" }
  password 'secret'
  password_confirmation 'secret'

  factory :normal_user do
    after_create {|u| Factory(:assignment, :user => u)}
  end

  factory :admin_user do
    after_create {|u| Factory(:assignment, :role => Factory(:admin_role), :user => u)}
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文