(手抜きエントリ)モデルのバリデーションのテストコードをRspecで書いてみた

こんにちは!kossyです!




本日のニュースでは無いですが、個人的に注目のニュースです。
jp.techcrunch.com

VoicyのIT公式ニュースでメドレー社の存在を知りました。

ヘルスケア業界に少しでも関わる自分にとっては、
技術をオープン化し、医療業界の無駄な部分を改善していきたいというビジョンには
共感を覚えます。

これからも動向を注視したいです。










さて、今回はRspecでバリデーションのテストコードを書いてみたので、
ブログに残してみたいと思います。



環境
Rails 5.1.6
Ruby 2.5.1
Rspec 3.8.0
MacOS Mojave





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

name, email, passwordカラムにバリデーションをかけています。

app/models/user.rb


  validates :name, presence: true, length: { maximum: 50 }
  validates :email, presence: true, uniqueness: true, format: {
    with: /\A[a-zA-Z0-9_\#!$%&`'*+\-{|}~^\/=?\.]+@[a-zA-Z0-9][a-zA-Z0-9\.-]+\z/
    }
  validates :password, presence: true, length: { minimum: 8 }, format: {
    with: /\A[a-z0-9]+\z/,
    message: 'は半角英数字で入力してください'
    }



user_spec.rbの記述

require 'rails_helper'

RSpec.describe User, type: :model do

  describe 'ユーザー作成のテスト' do
    context '名前・メールアドレス・パスワードがあれば有効な状態である場合' do
      it 'ユーザーは有効である' do
        user = User.new(
          name: 'test_user1',
          email: '12345678@gmail.com',
          password: 12345678,
          )
        expect(user).to be_valid
      end
    end
    describe 'validates :name, presence: true' do
      context '名前がnilの場合' do
        it 'ユーザーは無効である' do
          user = User.new(
            name: nil,
            email: '12345678@gmail.com',
            password: 12345678,
            )
          expect(user).not_to be_valid
        end
      end
    end
    describe 'validates :name, length: { maximum: 50 }' do
      context '名前が50文字以上の場合' do
        it 'ユーザーは無効である' do
          user = User.new(
            name: 'abcdefghijklmnopqestuvwxyzabcdefghijklmnopqestuvwxyz',
            email: '12345678@gmail.com',
            password: 12345678,
            )
          expect(user).not_to be_valid
        end
      end
    end
    describe 'validates :email format' do
      context 'メールアドレスが有効であるかどうか' do
        it '@がないのでメールアドレスは無効である' do
          user = User.new(
            name: 'test_user1',
            email: '12345678gmail.com',
            password: 12345678,
            )
          expect(user).not_to be_valid
        end
        it 'ひらがなが使用されているのでメールアドレスは無効である' do
          user = User.new(
            name: 'test_user1',
            email: 'あああああああ@gmail.com',
            password: 12345678,
            )
          expect(user).not_to be_valid
        end
        it 'ひらがなが使用されているのでメールアドレスは無効である' do
          user = User.new(
            name: 'test_user1',
            email: 'あああああああ@gmail.com',
            password: 12345678,
            )
          expect(user).not_to be_valid
        end
        it '@の位置が先頭なのでメールアドレスは無効である' do
          user = User.new(
            name: 'test_user1',
            email: '@12345678@gmail.com',
            password: 12345678,
            )
          expect(user).not_to be_valid
        end
      end
    end
    describe ':password, length: { minimum: 8 }' do
      it 'パスワードが4文字なので無効である' do
        user = User.new(
            name: 'test_user1',
            email: '12345678@gmail.com',
            password: 1234,
            )
          expect(user).not_to be_valid
      end
    end
  end
end

RspecってどこまでDRYに書けばいいのかわからないっすね。。。



参考にさせていただいた記事
Read Everyday Rails - RSpecによるRailsテスト入門 | Leanpub