FactoryGirl.define do
factory :agency do
name "Example Inc"
available_items "20"
recruiter # recruiter.id
end
factory :recruiter do
email 'example@example.com'
password 'please'
password_confirmation 'please'
# required if the Devise Confirmable module is used
# confirmed_at Time.now
end
end
agency.rb
class Agency < ActiveRecord::Base
belongs_to :recruiter
validates :name, :presence => true
end
recruiter.rb
class Recruiter < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :agency_attributes, :first_name
has_one :agency, :dependent => :destroy
accepts_nested_attributes_for :agency
validates :email, :presence => true
end
authentication_steps.rb
def create_user
@recruiter = FactoryGirl.create(:recruiter)
end
我如何复制此Recruiter&amp;使用factory_girl的代理商协会?
答案 0 :(得分:4)
我认为您应该从代理商工厂中删除招聘人员并将招聘代理添加到招聘人员因素
FactoryGirl.define do
factory :agency do
name "Example Inc"
available_items "20"
factory :agency_without_recuiter do
recuiter_id = 1
end
factory :agency_with_recuiter do
recuiter
end
end
factory :recuiter do
email 'example@example.com'
password 'please'
password_confirmation 'please'
factory :recuiter_with_agency
agency
end
end
end
这应该来自双方
create(:agency).recuiter => nil
create(:agency_with_recuiter).recuiter => recuiter
create(:recuiter).agency => nil
create(:recuiter_with_agency).agency => agency
希望它会有用。祝你好运!
答案 1 :(得分:1)
我认为你必须在你的测试用例中复制它,而不是在FG本身。
before (:each) do
@recruiter = FactoryGirl.create(:recruiter)
@agency = FactoryGirl.create(:agency)
@agency.recruiter = @recruiter
end
像这样。