LoginSignup
1
0

More than 1 year has passed since last update.

【Rails6】友達登録機能(ユーザーフォロー機能)の単体テストの実装例

Last updated at Posted at 2021-03-28

※2022年から技術系の記事は個人ブログに投稿しております。ぜひこちらもご覧ください→yamaday0u Blog

以前、別の記事で実装方法を紹介した友達登録機能の単体テストの実装例を紹介します。
【Rails6】友達登録機能(ユーザーフォロー機能)の実装方法

目次

  • 前提条件
  • Modelの確認
  • 単体テストのコードの実装

前提条件

  • RSpecをインストール済み。
  • 前回の記事で紹介した通りに友達登録機能を実装していること。

Modelの確認

前回の記事からの再掲です。

app/models/relationship.rb
# アソシエーションの定義
class Relationship < ApplicationRecord
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  # バリデーションの定義
  validates :follower_id, presence: true
  validates :followed_id, presence: true
  validates :follower_id, uniqueness: { scope: :followed_id }
end
app/models/user.rb
class User < ApplicationRecord
  # メソッドの定義
  # ユーザーをフォロー
  def follow(other_user)
    active_relationships.create(followed_id: other_user.id)
  end
end

単体テストのコードの実装

spec/models/relationship_spec.rb
require 'rails_helper'

RSpec.describe Relationship, type: :model do
  before do
    # ユーザー2名を事前に生成して登録
    @user = FactoryBot.create(:user)
    @another_user = FactoryBot.create(:user)
    # app/models/user.rbで定義したfollowメソッド(友達申請)の実行結果を変数@relationshipに代入
    @relationship = @user.follow(@another_user)
  end

  describe '#create' do
    context 'successfully' do
      it 'is valid with follower_id, followed_id' do
        expect(@relationship).to be_valid
      end
    end

    context 'unsuccessfully' do
      # 友達申請する側の値(follower_id)がなければ保存できない。
      it 'is invalid without follower_id(active relationship)' do
        @relationship.follower_id = ''
        @relationship.valid?
        expect(@relationship.errors.full_messages).to include('Follower must exist')
      end
      # 友達申請される側の値(followed_id)がなければ保存できない。
      it 'is invalid without followed_id(passive relationship' do
        @relationship.followed_id = ''
        @relationship.valid?
        expect(@relationship.errors.full_messages).to include('Followed must exist')
      end
      # 同じ組み合わせの友達申請のデータがすでに保存されている場合は保存できない
      it 'is invalid with duplicate relationship' do
        @relationship.save
        # @relationshipとは別のレコードとしてanother_relationshipを用意
        another_relationship = @user.follow(@another_user)
        # another_relationshipに@relationshipと同じ値を代入
        another_relationship.follower_id = @relationship.follower_id
        another_relationship.followed_id = @relationship.followed_id
        another_relationship.valid?
        expect(another_relationship.errors.full_messages).to include('Follower has already been taken')
      end
    end
  end
end

テストコードの最後の「同じ組み合わせの友達申請のデータがすでに保存されている場合は保存できない」は、app/models/relationship.rbで定義した以下のバリデーションが機能しているかを確認しています。

app/models/relationship.rb
  validates :follower_id, uniqueness: { scope: :followed_id }
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0