LoginSignup
7

More than 3 years have passed since last update.

Rails チュートリアル(7章)をRSpecでテスト

Last updated at Posted at 2019-08-22

はじめに

以前書いた記事です!
Rails チュートリアル(3章、4章、5章)をRSpecでテスト
Rails チュートリアル(6章)をRSpecでテスト

追加しました。
Rails チュートリアル(8章)をRSpecでテスト

7章のテストではSystem SpecRequest Specどっちで書けばいいかわからず、結局、viewが絡みそうなところはSystem Specでユーザーが登録出来たなどの見えない部分はRequest Specで書きました。

最初、Request Specにまとめて書いていたのですが、assignsと assert_templateが非推奨と書いてあって、新しいアプリにrails-controller-testinggemを入れるをオススメしないと...
render_templateが使えなくなり、他の書き方が思いつかなかったのでテストを2つに分けました。

参考リンク↓
http://rspec.info/blog/2016/07/rspec-3-5-has-been-released/#rails-support-for-rails-5

render_template↓
Project: RSpec Rails 3.8 -Render_template matcher

テストを書く(7章)

User Specテスト(有効なリクエストと無効なリクエスト)

spec/requests/access_to_users_spec.rb

describe 'POST #create' do
    #有効なユーザーの検証
    context 'valid request' do
      #ユーザーが追加される
      it 'adds a user' do
        expect do
          post signup_path, params: { user: attributes_for(:user) }
        end.to change(User, :count).by(1)
      end
      #ユーザーが追加されたときの検証
      context 'adds a user' do
        before { post signup_path, params: { user: attributes_for(:user) } }
        subject { response }
        it { is_expected.to redirect_to user_path(User.last) } #showページにリダイレクトされる
        it { is_expected.to have_http_status 302 } #リダイレクト成功
      end
    end
    #無効なリクエスト
    context 'invalid request' do
      #無効なデータを作成
      let(:user_params) do
        attributes_for(:user, name: '',
                              email: 'user@invalid',
                              password: '',
                              password_confirmation: '')
      end
      #ユーザーが追加されない
      it 'does not add a user' do
        expect do
          post signup_path, params: { user: user_params }
        end.to change(User, :count).by(0)
      end
    end
  end

ユーザーが追加されない検証のテストでrender_templateを使ってエラー時にnewテンプレートが渡されることを検証したかったのですが、上記の理由により出来なかったので、エラーの検証、エラー時に返すviewの検証、flash messageの検証はSystem Specに書きました。

System Spec(エラー、フラッシュメッセージなど)

spec/systems/users_spec.rb

require 'rails_helper'

RSpec.describe 'users', type: :system do
  describe 'user create a new account' do
    #有効な値が入力されたとき
    context 'enter an valid values' do
      before do
        visit signup_path
        fill_in 'Name', with: 'testuser'
        fill_in 'Email', with: 'testuser@example.com'
        fill_in 'Password', with: 'password'
        fill_in 'Confirmation', with: 'password'
        click_button 'Create my account'
      end
      #フラッシュメッセージが出る
      it 'gets an flash message' do
        expect(page).to have_selector('.alert-success', text: 'Welcome to the Sample App!')
      end
    end
    #無効な値が入力されたとき
    context 'enter an invalid values' do
      before do
        visit signup_path
        fill_in 'Name', with: ''
        fill_in 'Email', with: ''
        fill_in 'Password', with: ''
        fill_in 'Confirmation', with: ''
        click_button 'Create my account'
      end
      subject { page }
      #エラーの検証
      it 'gets an errors' do
        is_expected.to have_selector('#error_explanation')
        is_expected.to have_selector('.alert-danger', text: 'The form contains 6 errors.')
        is_expected.to have_content("Password can't be blank", count: 2)
      end
      #今いるページのURLの検証
      it 'render to /signup url' do
        is_expected.to have_current_path '/signup'
      end
    end
  end
end

7章おわりです。

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
7