dimanche 24 avril 2016

Rspec for Michael Hartl example of relationship following users

I'm trying to make a spec test for the MHartl's example. As it follows bellow my difficulties are for #Follows a user, #Unfollows a user and #Returns true if the current user is following the other user.

class User < ActiveRecord::Base

  # Associations
  has_many :active_relationships, class_name:  "Relationship",
                                  foreign_key: "follower_id",
                                  dependent:   :destroy
  has_many :following, through: :active_relationships, source: :followed
  has_many :followers, through: :passive_relationships, source: :follower


  # Follows a user.
  def follow(other_user)
    active_relationships.create(followed_id: other_user.id)
  end

  # Unfollows a user.
  def unfollow(other_user)
    active_relationships.find_by(followed_id: other_user.id).destroy
  end

  # Returns true if the current user is following the other user.
  def following?(other_user)
    following.include?(other_user)
  end

If you need more info about this post, please fell free to ask! and please tell me were can I learn more about this specs tests. thanks for your help :)

Aucun commentaire:

Enregistrer un commentaire