mardi 31 mai 2016

Rails test and fixture are not matching

Suppose we have the following models. I have a notion of a user and a band. Users can be part of bands through memberships. Now, there is a notion of a "Notification", which for now is effectively being used to invite members to join a band. A Notification has both a User (effectively the invitee), the Band, and a Creator (should be an admin Member)

class User < ActiveRecord::Base
  has_many :members, dependent: :destroy
  has_many :bands, through: :members
  has_many :notifications
end

class Notification < ActiveRecord::Base
  belongs_to :band
  belongs_to :user,    class_name: "User", foreign_key: "user_id"
  belongs_to :creator, class_name: "User", foreign_key: "user_id"
end

class Band < ActiveRecord::Base
  has_many :members
  has_many :users, through: :members, dependent: :destroy
  validates :name, presence: true, uniqueness: { case_sensitive: false }
end

class Member < ActiveRecord::Base
  belongs_to :user
  belongs_to :band
end

Now suppose we have this record in notifications.yml, where chris and alexander are two different users and h4h is a given band. Suppose that chris has a name column of "Chris Cheveldayoff" and alexander's name is "Alexander Tester"

chris_h4h:
  notification_type: invite
  user:              chris
  creator:           alexander
  band:              h4h
  has_expired:       false

I am writing a test to test a precondition whereby you cannot create an invitation to a given user if such an invitation exists. I'm confident it works correctly in code, but it is failing in the test. I looked into it further and noticed something fishy, and was wondering why. Suppose @chris_not = notifications(:chris_h4h).

puts @chris_not.user.name
puts @chris_not.creator.name

For some reason, both print out "Alexander Tester", and I'm stumped to why this is the case.

Aucun commentaire:

Enregistrer un commentaire