LoginSignup
0
1

More than 3 years have passed since last update.

【備忘録】Rspecのはじめ方

Last updated at Posted at 2020-10-21

本稿を作るにあたり

RubyonRailsでアプリケーションを開発した後、別のアプリケーションに用いる際の備忘録(2020年10月21日時点Mac)

自分は取り掛かるまでが長かったので、ひとまず感覚を掴むためによかったらやってみてください。

※詳細な内容について、別記事を参照されたし

Rspecを行う際、使用したGem

Gemfile
group :test do
  gem 'capybara', '>= 2.15'
  gem 'rspec-rails'
  gem "factory_bot_rails"
  gem 'faker'
end
ターミナル
bundle install

rspec-rails

Rspecを実行するためのGem
Ruby on Rails のテストフレームワーク RSpec 事始め
https://qiita.com/tatsurou313/items/c923338d2e3c07dfd9ee

capybara

ドライバの設定をしないとRspecが動かなかったため、導入したGem
Capybaraチートシート
https://qiita.com/morrr/items/0e24251c049180218db4

faker

データを作成するためのGem。主にRspecを実行している際の画面を入力テストのテストデータをGem内のライブラリを用いてランダムに作成していくれる。seedファイルで仮データも入力でき、便利。
Fakerを使ってみました!(使い方と実行例)
https://qiita.com/ginokinh/items/3f825326841dad6bd11e

factory_bot_rails

先にテストデータを作成しておけるGem、主にModelのデバックに使いました。
【Rails】factory_botの使い方メモ
https://qiita.com/at-946/items/aaff42e4a9c2e4dd58ed

Rspecのインストール

ターミナル
rails generate rspec:install

インストールすると下記ファイルが作成されます。

ターミナル
  Running via Spring preloader in process 9045
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

spec_helper.rbにドライバの設定

仕組み的なところは未理解、おそらくここの設定のおかげでブラウザと同じ環境でプログラムを走らせることができる。

spec_helper.rb
require 'capybara/rspec'
RSpec.configure do |config|
    config.before(:each, type: :system) do
    #driven_by :selenium_chrome_headless
    driven_by :rack_test
    end
end

以後実際にテスト用のプログラムを記述

factoriesフォルダで作成するデータを作成、modelがバリテーション等のテストができ、sysyemで実際の画面上の動作をテストできる。参考までにコードを記載
※deviseを用いた新規ユーザー登録のチェックを想定
スクリーンショット 2020-10-21 20.58.33.png

factories/users_spec.rb
FactoryBot.define do
  factory :user do
    email { Faker::Internet.email }
    password { 'password' }
    password_confirmation { 'password' }
  end
end
model/user_spec.rb
require 'rails_helper'

RSpec.describe 'Userモデルのテスト', type: :model do
  before do
    @user = build(:user)
  end

  describe 'バリデーション' do
    it 'emailが空だとNG' do
      @user.email = ''
      expect(@user.valid?).to eq(false)
    end
  end
end
system/user_spec.rb
require 'rails_helper'

describe 'ユーザー認証のテスト' do
  describe 'ユーザー新規登録' do
    before do
      visit new_user_registration_path
    end
    context '新規登録画面に遷移' do
      it '新規登録に成功する' do
        fill_in 'user[email]', with: Faker::Internet.email
        fill_in 'user[password]', with: 'password'
        fill_in 'user[password_confirmation]', with: 'password'
        click_button "sign in"                       #ボタンに表示されている文字
        expect(page).to have_content 'successful'   #ログイン後に表示されるメッセージを指定してください
      end
    end
  end
end

Rspecを実行

下記コマンドでspecフォルダ内のテストが全て実行され、成功したものが緑、失敗したものが赤でターミナル上に表示される。

ターミナル
bundle exec rspec spec/ --format documentation

ESpecチートシート
https://qiita.com/morrr/items/6ed889d17abbfd352649

以上、お疲れ様でした。

0
1
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
1