我刚刚开始尝试Rails测试。我无法入门。如何使用FactoryGirl建立关联以测试Rails应用程序
我的第一个测试需要一个用户,我在FactoryGirl内创建。但是我的模型验证了邀请的存在。
class User
has_many :sent_invitations, :class_name => 'Invitation', :foreign_key => 'sender_id'
belongs_to :invitation
validates_presence_of :invitation_id
end
class Invitation
belongs_to :sender, :class_name => 'User'
has_one :recipient, :class_name => 'User'
end
如何使用FactoryGirl生成受邀用户?
我试过几种方法,但没有设置关联。似乎最有可能的工作之一是这样的:
factory :invitation do
token "MyInvitationToken"
recipient_email "[email protected]"
factory :invited_user do
association :user, :factory => :user
end
end
factory :user do
email "[email protected]"
end
这与Validation failed: Invitation is required
失败。
与FactoryGirl建立此关联的正确方法是什么?
对于奖励积分,我该如何修改它以便:invitation.recipient_email是一个序列sequence(:recipient_email) { |n| "test#{n}@example.com" }
,并且该值将传递到invite_user的每个迭代的email列。
http://rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Associations – konyak 2014-07-23 21:03:34