邮件程序关联在邮件程序测试中失败

时间:2015-07-06 21:43:28

标签: ruby-on-rails ruby ruby-on-rails-4 testing associations

邀请与组织(很多:1)和用户(很多:很多)相关联。这在开发中有效。我可以创建一个新邀请,发送带有邀请的电子邮件。此邀请的值为person_one_idperson_two_idorganization_id。 Person_one和person_two是User模型中的用户。这在开发中有效,控制台可以正确返回Invitation.first.organizationInvitation.first.person_oneInvitation.first.person_two的记录。

问题在于我的邮件测试。虽然从灯具加载的邀请具有person_one_idperson_two_idorganization_id的值,但如果在调试器中我呼叫invitation.organizationinvitation.person_one或{{ 1}},它返回invitation.person_two

有没有人看到导致此问题的测试代码出了什么问题?

邮件程序:

nil

照明灯:

def invitation(invitation)
  @invitation = invitation
  @organization = invitation.organization   # This returns nil even though invitation has a value for organization_id
  ...etc...
end

邮件测试:

invitation_one:
  person_one: users(:one)                 # Loads this person from users fixtures.
  person_two: users(:archer)
  organization: organizations(:one)       # Loads from organizations fixtures.
  message: I am pleased to invite you1

在加载灯具的行之后,我在测试文件中添加了调试器。调试器使用:

回复test "invitation" do @invitation = invitations(:invitation_one) mail = InvitationMailer.invitation(@invitation) # Produces a "NoMethodError: undefined method `id' for nil:NilClass" error since a.o. invitation.organization is nil. ...etc... end
@invitation

这证明了关联(<Invitation id: 749093126, person_one_id: 41652760, person_two_id: 976142315, organization_id: 118162230, email: nil, message: "I am pleased to invite you1", created_at: "2015-07-05 09:06:23", updated_at: "2015-07-07 12:27:19"> person_one_idperson_two_id)具有值。不过,调试器会使用nill响应organization_id,而@invitation.organization@invitation.person_one也会这样做。

1 个答案:

答案 0 :(得分:0)

问题原来是固定装置的设置。要为我需要使用的灯具创建关联:

invitation_one:
  person_one: one
  person_two: archer
  organization: one
  message: I am pleased to invite you1