2014-10-01 79 views
0

我有2類用戶和認證,然後認證的has_many用戶:HAS_MANY協會FactoryGirl

用戶等級:

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 

,我現在不。如何創建用戶身份驗證?

+0

請看看到[協會(https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations )入門指南部分 – wik 2017-02-17 13:30:09

回答

0

要建立在規範協會我使用以下命令:

# 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 } 
. . .