LoginSignup
0
2

More than 1 year has passed since last update.

Rspecの導入

Last updated at Posted at 2020-02-11

Rspecとは

Ruby on Railsの開発案件で主流のテストフレームワーク。Rails標準では簡易的なminitestがデフォルトで使用されているが、実際にはより高機能なRspecを採用するケースの方が多い。

Gemfileのテスト環境にRspecを追加する。

.
.
group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  #gem 'chromedriver-helper' #これをコメントアウトまたは削除
  gem 'webdrivers' #これと
  gem 'rspec-rails' #これを追加
end
.
.

Rspecを使用するために必要なディレクトリやファイルを作成する。

$ bin/rails g rspec:install



Rails標準のテストツールであるminitestのファイル一式を削除しておく。

$ rm -r ./test



spec_helperにテストツールの設定を記載

spec/spec_helper.rb
require 'capybara/rspec'  #テストにcapybaraを使用できるようにする。

RSpec.configure do |config|
  # テスト用ブラウザにselenium_headless_chrome(画面のないブラウザ)を使用するように設定
  config.before(:each, type: :system) do
    driven_by :selenium_chrome_headless
  end
.
.


FactoryBotの導入

factory_botはテスト用データの作成のためのツール


Gemfileに記述

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'factory_bot_rails' #これを追加
end

userのテストデータを作成する。

$ mkdir spec/factories && touch spec/factories/users.rb
spec/factories/users.rb
FactoryBot.define do
 factory :user do
     user_name { 'factoryユーザー' }
     email { 'factory@example.com' }
     password { 'password' }
 end
end


memoのテストデータを作成する。
$ touch spec/factories/memos.rb
spec/factories/memos.rb
FactoryBot.define do
    factory :memos do
        title { 'Ruby' }
        memo_text { 'Rspecの勉強' }
        user #関連するuserの設定がされていればこの記載だけでuser_idを推測してくれる。
    end
end

メモの一覧画面表示のテストを行う

$ mkdir spec/system && touch spec/system/memos_spec.rb
app/arocam/sample_app/spec/system/memos_spec.rb
require 'rails_helper'

describe 'メモ管理', type: :system do
    describe '一覧表示機能' do
        # テスト実施前の事前準備
        before do
            #ユーザーを作成しておく(users.rbで定義したuser情報を作成する。)
            user1 = FactoryBot.create(:user, user_name: 'ユーザー1', email: 'user1@example.com')
            #作成者がユーザーAであるメモを作成しておく
            FactoryBot.create(:memo, title: 'user1のメモ', memo_text: "テキストテキストテキスト", user: user1)
        end

        context 'ユーザー1がログインしている時' do
            before do
                #ユーザー1でログインする。
                visit login_path
                fill_in "session[email]", with: 'user1@example.com'
                fill_in "session[password]", with:'password'
                click_button 'ログインする'
            end

            it 'ユーザー1が作成したメモが表示されること' do
                # 期待する結果を記載
                expect(page).to have_content 'user1のメモ'
            end
        end

    end
end

テスト用ブラウザの用意

ブラウザでの動作確認もテストコードで行えるように仮想環境にGoogleChromeをインストールしておく

GoogleChromeをインストールする。

$ sudo vim /etc/yum.repos.d/google.chrome.repo
google.chrome.repo
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
$ sudo yum install google-chrome-stable
$ sudo yum install -y libOSMesa google-noto-cjk-fonts



chromeがインストールできたか確認する

$ google-chrome --version
Google Chrome 80.0.3987.87 #chrmoeの情報が表示されればOK

テストを実行する。

$ bundle exec rspec spec/system/memos_spec.rb

Capybara starting Puma...
* Version 3.12.2 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:37913
.

Finished in 17.72 seconds (files took 4.34 seconds to load)
1 example, 0 failures

1 example, 0 failures
のように表示されればテスト成功

0
2
1

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