이것은 아마도 간단하지만 어딘가에서 예제를 찾을 수 없습니다. 나는 프로필을 만들 때FactoryGirl은 연결된 객체의 속성을 무시합니다.
FactoryGirl.define do
factory :profile do
user
title "director"
bio "I am very good at things"
linked_in "http://my.linkedin.profile.com"
website "www.mysite.com"
city "London"
end
end
FactoryGirl.define do
factory :user do |u|
u.first_name {Faker::Name.first_name}
u.last_name {Faker::Name.last_name}
company 'National Stock Exchange'
u.email {Faker::Internet.email}
end
end
은 내가 사용자의 일부를 대체 할 일은 원하는 것은 속성 :
내가 두 개의 공장을 가지고
p = FactoryGirl.create(:profile, user: {email: "[email protected]"})
또는 이와 유사한
,하지만 난 할 수 없습니다 구문을 올바르게 얻으십시오. 오류 :ActiveRecord::AssociationTypeMismatch: User(#70239688060520) expected, got Hash(#70239631338900)
는 내가 프로필과 연결 먼저 사용자를 생성하고이 작업을 수행 할 수 있습니다 알고,하지만 난 더 나은 방법이 있어야합니다 생각했다.
또는이 작동합니다
p = FactoryGirl.create(:profile, user: FactoryGirl.create(:user, email: "[email protected]"))
하지만 지나치게 복잡한 것 같다. 연관된 속성을 재정의하는 더 간단한 방법은 없습니까? 올바른 구문은 무엇입니까 ??
고마워요.하지만 모든 속성에서 작동하고 싶습니다. 그래서 각 코드에 대해 코드를 작성하고 싶지는 않습니다. 아마 아무도 이걸 필요로하지 않을거야. – bobomoreno
당신의 예제에 오류가 있다고 생각합니다. 'after (: create)'를'profile.user.email = evaluator.user_email '로 변경하십시오 (evaluator.user_email.nil?가 아닌 경우). – Kelly