0
2

効率的にテストコードを書こう

Posted at

テストコードを効率的にできるのは便利ですね!

ruby

目的

  • FactoryBotの概要と設定の方法を理解
  • Fakerの概要と設定の方法を理解

インスタンスを生成するコードを分離

FactoryBot

インスタンスをまとめることができるGem。他のファイルであらかじめ各クラスのインスタンスに定める値を設定しておき、各テストコードで使用する

FactoryBotを導入

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem "debug", platforms: %i[ mri mingw x64_mingw ]
  gem 'rspec-rails', '~> 4.0.0'
  gem 'factory_bot_rails'
end

bundle install
specディレクトリ に factories/users.rb作成

FactoryBotを使用してインスタンス生成を共通化

spec/factories/users.rb

FactoryBot.define do
  factory :user do
    nickname              {'test'}
    email                 {'test@example'}
    password              {'000000'}
    password_confirmation {password}
  end
end

build

ActiveRecordのnewメソッドと同様の意味を持つ
spec/models/user_spec.rb

require 'rails_helper'
RSpec.describe User, type: :model do
  describe 'ユーザー新規登録' do
    it 'nicknameが空では登録できない' do
      user = FactoryBot.build(:user)  # Userのインスタンス生成
      user.nickname = ''  # nicknameの値を空にする
      user.valid?
      expect(user.errors.full_messages).to include "Nickname can't be blank"
    end
    it 'emailが空では登録できない' do
      user = FactoryBot.build(:user)  # Userのインスタンス生成
      user.email = ''  # emailの値を空にする
      user.valid?
      expect(user.errors.full_messages).to include "Email can't be blank"
    end
  end
end

共通した記述を切り出す

before

それぞれのテストコードを実行する前に、セットアップを行うことができます
spec/models/user_spec.rb

require 'rails_helper'
RSpec.describe User, type: :model do
  before do
    @user = FactoryBot.build(:user)
  end

  describe 'ユーザー新規登録' do
    it 'nicknameが空では登録できない' do
      @user.nickname = ''
      @user.valid?
      expect(@user.errors.full_messages).to include "Nickname can't be blank"
    end
    it 'emailが空では登録できない' do
      @user.email = ''
      @user.valid?
      expect(@user.errors.full_messages).to include "Email can't be blank"
    end
  end
end

beforeに定義する変数はインスタンス変数にする必要があります

ランダムな値の出力

Faker

ランダムな値を生成するGem

Faker導入

Gemfile

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem "debug", platforms: %i[ mri mingw x64_mingw ]
  gem 'rspec-rails', '~> 4.0.0'
  gem 'factory_bot_rails'
  gem 'faker'
end

bundle install

###Fakerを試す

rails c

[1] pry(main)> Faker::Name.initials(number: 2)
=> "XL"

[2] pry(main)> Faker::Internet.email
=> "georgina@yahoo.com"

[3] pry(main)> Faker::Internet.password(min_length: 6)
=> "9DfWbC3gZsZ5cL"

Fakerを使用した記述を変更

spec/factories/users.rb

FactoryBot.define do
  factory :user do
    nickname              {Faker::Name.initials(number: 2)}
    email                 {Faker::Internet.email}
    password              {Faker::Internet.password(min_length: 6)}
    password_confirmation {password}
  end
end
0
2
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
0
2