【问题标题】:Rspec (for Michael Hartl example) for the definition of relationship following usersRspec(用于 Michael Hartl 示例)用于定义跟随用户的关系
【发布时间】:2016-04-26 11:58:42
【问题描述】:

我正在尝试对MHartl's example 进行规格测试。 如下所示,我的困难在于#Follows 用户。

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

对于我的规范:

require 'rails_helper'

RSpec.describe User, :type => :model do
  let(:user) { build(:user) }

  describe 'Validations' do
    it 'has a valid factory' do
      expect(user).to be_valid
    end

    it { should validate_presence_of(:email) }
    it { should validate_presence_of(:password) }
    it { should validate_confirmation_of(:password) }
  end


  let(:user) { create(:user) }
  let(:other_user) { create(:user) }

  describe '#following?' do
    it "expect relationship between two users to be empty" do
      expect(user.active_relationships).to be_empty
    end
  end

  describe '#follow' do
    it "creates the active relationship between two users" do
      user.follow(other_user)
      expect(user.active_relationships.first.followed_id).to eq(other_user.id)
    end

    it "creates the passive relationship between two users" do
      user.follow(other_user)
      expect(other_user.passive_relationships.first.follower_id).to eq(user.id)
    end
  end

  describe '#unfollow' do
    it "destroys the active relationship between two users" do
      user.follow(other_user)
      user.unfollow(other_user)
      expect(user.active_relationships.find_by.followed_id).to change(Relationship, :count).by(-1)
    end
  end

我的失败:

1) User#unfollow destroys the active relationship between two users
     Failure/Error: active_relationships.find_by(followed_id: other_user.id).destroy

     NoMethodError:
       undefined method `destroy' for nil:NilClass

  2) User#follow creates the passive relationship between two users
     Failure/Error: expect(other_user.passive_relationships.first.follower_id).to eq(user.id)

     NoMethodError:
       undefined method `passive_relationships' for #<User:0x0000010c4146e8>

如果您需要有关此帖子的更多信息,请随时询问! 请告诉我,我可以了解更多有关此规格测试的信息吗? 谢谢你的帮助:)

【问题讨论】:

    标签: ruby-on-rails unit-testing rspec refactoring


    【解决方案1】:

    您在 User 模型中缺少 :passive_relationships 关系,该模型反映了 :active_relationships 关系:

    has_many :passive_relationships, class_name:  "Relationship",
                                     foreign_key: "followed_id",
                                     dependent:   :destroy
    

    【讨论】:

      【解决方案2】:

      最好在let 中创建此其他用户,并将build 替换为create,因为您没有验证新的未保存记录:

      let(:user) { create(:user) }
      let(:other_user) { create(:user) }
      

      然后

      user.follow(other_user)
      

      【讨论】:

      • 您好,我刚刚在您的帮助下更新了!但我得到了错误:You cannot call create unless the parent is saved
      • 测试适用于user.follow(other_user),但不适用于user.following?(other_user).should be_valid
      • 嘿@Малъ Скрылевъ,我遇到了其他问题...你能看看吗?谢谢
      • @Jony 你没有passive_relationships,用和active_relationships一样的方式添加就行了
      • @Jony 表明没有 active_relationships 与 follow_id 作为 other_user.id
      猜你喜欢
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      相关资源
      最近更新 更多