lundi 6 juillet 2015

Associations in fixtures for mailer test don't seem to work

My mailer test does not seem to load its fixtures correctly.

My mailer:

def invitation(invitation)
  @invitation = invitation
  @organization = invitation.organization
  if invitation.person_one.nil?
    @person_one = User.where(organization_id: @organization.id).where(admin: true).first
  else
    @person_one = invitation.person_one
  end
  @person_two = invitation.person_two
  mail to: [invitation.person_two.email, @person_one.email]
  mail subject: "subject"
end

Fixtures invitations.yml:

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

Mailer test:

test "invitation" do
  @invitation = invitations(:invitation_one)
  mail = InvitationMailer.invitation(@invitation)
  assert_equal "subject", mail.subject
  assert_equal ["one@example.com"], mail.to
  assert_match "I am pleased to invite you1", mail.body.encoded
end

This test fails with the message NoMethodError: undefined method 'id' for nil:NilClass, referring to the @person_one = User.where ... line in the mailer. So @organization turn out to be nil, and invitation.person_one must also be nil. But these values are specified in the fixtures... Why are they nil?

Aucun commentaire:

Enregistrer un commentaire