繁体   English   中英

在FactoryGirl中具有Has_many协会

[英]Has_many Association in FactoryGirl

我有2类用户和身份验证,然后身份验证has_many用户:

用户类别:

FactoryGirl.define do
  factory :user do
    first_name   "Juan"
    last_name    "Iturralde"
    sequence(:email)  { |n| "person-#{n}@example.org" }
    password  "1234567890"
    password_confirmation "1234567890"
    is_admin false
    factory :admin do
        is_admin true
    end
    after(:create) do |user|
        create(:authentication, user: user)
    end
  end
end

认证类别:

FactoryGirl.define do
  factory :authentication do
    user        {User.first || create(:user)}
    provider    "Apple"
    uid         "uid"
  end
end

而且我现在不。 如何在身份验证中创建用户?

为了建立规范中的关联,我使用以下命令:

# spec/factories/posts.rb
FactoryGirl.define do
  factory :post do
    title 'The best post'

    trait :with_comments do
      create_list :comment, 3
    end
  end
end

# spec/factories/comments.rb
FactoryGirl.define do
  factory :comment do
    post
    content 'Really awesome post'
  end
end

# in specs
. . .
let(:post_with_commments) { create :post, :with_comments }
. . .

暂无
暂无

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

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