简体   繁体   中英

Fixture associations failing in mailer test

An Invitation is associated to Organization (many:1) and User (many:many through). This works in development. I can create a new Invitation, which sends an email message with an invitation. This invitation has values for person_one_id , person_two_id and organization_id . Person_one and person_two are users in the User model. This works in development, which is confirmed by the console correctly returning records for Invitation.first.organization , Invitation.first.person_one and Invitation.first.person_two .

The problem is with my mailer test. Although the invitation that is loaded from fixtures has values for person_one_id , person_two_id and organization_id , if in the debugger I call on invitation.organization , invitation.person_one or invitation.person_two , it returns nil .

Does anyone see what's wrong with my test code that is producing this problem?


Mailer:

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

Fixtures:

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

Mailer test:

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

I added the debugger in the test file after the line loading the fixtures. The debugger responded to @invitation with:

<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"> 

This proofs the associations ( person_one_id , person_two_id and organization_id ) have values. Nevertheless, the debugger responds to @invitation.organization with nill, and it does the same for @invitation.person_one and @invitation.person_two .

The problem turned out to be the fixtures set up. To create the associations for the fixtures I needed to use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM