LoginSignup
5
3

More than 3 years have passed since last update.

【Rails6】RSpecによるフォロー機能の結合テストの実装

Last updated at Posted at 2021-03-08

はじめに

サービスの品質を保つために必要不可欠なテストを実施しております。

今回はユーザーフォロー機能の結合テストを実装し、その実装内容を記事にしていきたいと思います。

前提

・Relationship(フォロー)モデルの単体テストは実施済み

※完了されていない方や単体テストと並行してご覧になられたい方は、以下のRelationshipモデルの単体テストについての記事をご参考ください。

バージョン

rubyのバージョン ruby-2.6.5
Railsのバージョン Rails:6.0.0
rspec-rails 4.0.0

実施したテスト

image.png

ユーザー一覧画面について

image.png

握手のマークをクリックすると非同期によるフォローが実行されます。

relationshipsテーブルのカラムの紹介

xxx_create_relationships.rb
class CreateRelationships < ActiveRecord::Migration[6.0]
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id
      t.timestamps
    end
  end
end

モデル内のバリデーション

app/models/relationship.rb
class Relationship < ApplicationRecord
  belongs_to :follower, class_name: 'User'
  belongs_to :followed, class_name: 'User'

  validates :follower_id, presence: true, uniqueness: { scope: :followed_id }
  validates :followed_id, presence: true
end

FactoryBotの内訳

spec/factories/rerationships.rb
FactoryBot.define do
  factory :relationship do
    follower_id    { FactoryBot.create(:user).id }
    followed_id    { FactoryBot.create(:user).id }
  end
end

サポートモジュール

spec/support/sign_in_support.rb
module SignInSupport
  def sign_in(user)
    visit user_session_path
    fill_in 'user[email]', with: user.email
    fill_in 'user[password]', with: user.password
    find('input[name="commit"]').click
    expect(current_path).to eq(root_path)
  end
end

テストコードの内容

spec/system/relationships_spec.rb
require 'rails_helper'

RSpec.describe "Relationships", type: :system do
before do
    @user1 = FactoryBot.create(:user)
    @user2 = FactoryBot.create(:user)
  end

    describe '#create,#destroy' do
        it 'ユーザーをフォロー、フォロー解除できる' do 
            # @user1としてログイン           
            sign_in(@user1)

            # @user1としてユーザー一覧ページへ遷移する
            visit users_path(@user1)

            # @user2をフォローする
            find('#no-follow').click
            expect(page).to have_selector '#yes-follow'
            expect(@user2.followed.count).to eq(1)
            expect(@user1.follower.count).to eq(1)

            # @user2をフォロー解除する
            find('#yes-follow').click
            expect(page).to have_selector '#no-follow'
            expect(@user2.followed.count).to eq(0)
            expect(@user1.follower.count).to eq(0)
        end
    end
end

補足説明

「user2をフォローする」のテストコードについて

下記の前半の行では、user1がuser2フォローすると、user2のフォロワー数が1上がるという意味の文法です。

一方、後半の行ではuser1がuser2フォローすると、user1のフォロー数が1上がるという意味の文法です。

spec/system/relationships_spec.rb
 expect(@user2.followed.count).to eq(1)
 expect(@user1.follower.count).to eq(1)

「user2をフォロー解除する」のテストコードについて

「user2をフォローする」のテストコードの逆の意味合いがあり、フォロー解除するとカウント数が1減るので0となるという意味の文法です。

spec/system/relationships_spec.rb
 expect(@user2.followed.count).to eq(0)
 expect(@user1.follower.count).to eq(0)

以上です。

5
3
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
5
3