From 9b52489c2257c4bdfcc7e27352e5f54ecac4b162 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Sun, 8 Oct 2023 12:48:04 +0900 Subject: [PATCH] =?UTF-8?q?RSpec=E7=94=A8rubocop=E8=A8=AD=E5=AE=9A,rubocop?= =?UTF-8?q?=E9=81=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .rubocop.yml | 4 ++++ spec/factories/user.rb | 4 +++- spec/models/user_spec.rb | 16 +++++++-------- spec/requests/posts_spec.rb | 27 ++++++++++++------------- spec/requests/users_spec.rb | 39 +++++++++++++++++++------------------ 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 62a36ca..ad069c8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -26,3 +26,7 @@ Hc/RailsSpecificActionName: Rails/LexicallyScopedActionFilter: Exclude: - app/controllers/users/registrations_controller.rb +RSpec/ContextWording: + AllowedPatterns: + - とき$ + - カラム$ \ No newline at end of file diff --git a/spec/factories/user.rb b/spec/factories/user.rb index 8eb2ba4..4cd9d3d 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :user do name { 'test_name' } @@ -8,4 +10,4 @@ phone { '123456789' } birthdate { '2000-01-01' } end -end \ No newline at end of file +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 9f43c07..6ef4470 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -6,28 +6,28 @@ describe 'バリデーション' do let(:user) { create(:user) } - context '正常系' do + context '正常系のとき' do it 'name,user_name,password,phone,birthdateが入力されている場合、登録できること' do user = build(:user) expect(user).to be_valid end end - context "nameカラム" do + context 'nameカラム' do it '空欄の場合、登録できないこと' do user.name = '' expect(user).to be_invalid end end - context "user_nameカラム" do + context 'user_nameカラム' do it '空欄の場合、登録できないこと' do user.user_name = '' expect(user).to be_invalid end end - context "emailカラム" do + context 'emailカラム' do it '空欄の場合、登録できないこと' do user.email = '' expect(user).to be_invalid @@ -39,7 +39,7 @@ end end - context "passwordカラム" do + context 'passwordカラム' do it '空欄の場合、登録できないこと' do user.password = '' expect(user).to be_invalid @@ -52,7 +52,7 @@ end end - context "password_confirmationカラム" do + context 'password_confirmationカラム' do it '空欄の場合、登録できないこと' do user.password_confirmation = '' expect(user).to be_invalid @@ -65,14 +65,14 @@ end end - context "phoneカラム" do + context 'phoneカラム' do it '空欄の場合、登録できないこと' do user.phone = '' expect(user).to be_invalid end end - context "birthdateカラム" do + context 'birthdateカラム' do it '空欄の場合、登録できないこと' do user.birthdate = '' expect(user).to be_invalid diff --git a/spec/requests/posts_spec.rb b/spec/requests/posts_spec.rb index c54e925..0d433cc 100644 --- a/spec/requests/posts_spec.rb +++ b/spec/requests/posts_spec.rb @@ -4,49 +4,50 @@ RSpec.describe '`Posts', type: :request do describe 'ツイート:POST posts#create' do - let(:user) { create(:user) } + context '正常系:パラメータ正常のとき' do + let(:user) { create(:user) } + let(:post_params) { { content: 'テスト投稿' } } - context '正常系:パラメータ正常' do before do user.confirm sign_in user - @post_params = { content: 'テスト投稿' } end it 'requestが成功すること' do - post posts_path, params: { post: @post_params }, headers: { HTTP_REFERER: root_url } - expect(response).to have_http_status(302) + post posts_path, params: { post: post_params }, headers: { HTTP_REFERER: root_url } + expect(response).to have_http_status(:found) end it 'ツイートcreateに成功すること' do expect do - post posts_path, params: { post: @post_params }, headers: { HTTP_REFERER: root_url } + post posts_path, params: { post: post_params }, headers: { HTTP_REFERER: root_url } end.to change(Post, :count).by 1 end it 'root_pathにリダイレクトされること' do - post posts_path, params: { post: @post_params }, headers: { HTTP_REFERER: root_url } + post posts_path, params: { post: post_params }, headers: { HTTP_REFERER: root_url } expect(response).to redirect_to root_url end end - context '異常系:パラメータ不正' do + context '異常系:パラメータ不正のとき' do + let(:user) { create(:user) } + let(:post_params) { { content: '' } } + before do user.confirm sign_in user - @post_params = { content: '' } end it 'requestが成功すること' do - post posts_path, params: { post: @post_params } - expect(response).to have_http_status(200) + post posts_path, params: { post: post_params } + expect(response).to have_http_status(:ok) end it 'responseにエラー文が含まれること' do - post posts_path, params: { post: @post_params } + post posts_path, params: { post: post_params } expect(response.body).to include 'エラーが発生したため 投稿 は保存されませんでした。' end end end - end diff --git a/spec/requests/users_spec.rb b/spec/requests/users_spec.rb index 583d8e9..e50f995 100644 --- a/spec/requests/users_spec.rb +++ b/spec/requests/users_spec.rb @@ -6,12 +6,12 @@ describe 'サインアップ:POST devise/registrations#create' do let(:user) { create(:user) } let(:user_params) { attributes_for(:user) } - let(:invalid_user_params) { attributes_for(:user, name:'') } + let(:invalid_user_params) { attributes_for(:user, name: '') } - context '正常系:パラメータ正常' do + context '正常系:パラメータ正常のとき' do it 'requestが成功すること' do post user_registration_path, params: { user: user_params } - expect(response).to have_http_status(303) + expect(response).to have_http_status(:see_other) end it '認証メールが送信されること' do @@ -31,10 +31,10 @@ end end - context '異常系:パラメータ不正' do + context '異常系:パラメータ不正のとき' do it 'requestが成功すること' do post user_registration_path, params: { user: user_params } - expect(response).to have_http_status(303) + expect(response).to have_http_status(:see_other) end it '認証メールが送信されること' do @@ -45,7 +45,7 @@ it 'ユーザcreateに失敗すること' do expect do post user_registration_path, params: { user: invalid_user_params } - end.to_not change(User, :count) + end.not_to change(User, :count) end it 'responseにエラー文が含まれること' do @@ -56,46 +56,47 @@ end describe 'ログイン:POST devise/sessions#create' do - let(:user) { create(:user) } + context '正常系:パラメータ正常のとき' do + let(:user) { create(:user) } + let(:user_params) { { email: user.email, password: user.password } } - context '正常系:パラメータ正常' do before do user.confirm - @user_params = { email: user.email, password: user.password } end it 'requestが成功すること' do - post user_session_path, params: { user: @user_params } - expect(response).to have_http_status(303) + post user_session_path, params: { user: user_params } + expect(response).to have_http_status(:see_other) end it 'ログイン状態になること' do - post user_session_path, params: { user: @user_params } + post user_session_path, params: { user: user_params } expect(controller.user_signed_in?).to be true end it 'root_pathにリダイレクトされること' do - post user_session_path, params: { user: @user_params } + post user_session_path, params: { user: user_params } expect(response).to redirect_to root_url end end - context '異常系:パラメータ不正' do + context '異常系:パラメータ不正のとき' do + let(:user) { create(:user) } + let(:invalid_user_params) { { email: user.email, password: 'invalid' } } + before do user.confirm - @invalid_user_params = { email: user.email, password: 'invalid' } end it 'requestが失敗すること' do - post user_session_path, params: { user: @invalid_user_params } - expect(response).to have_http_status(422) + post user_session_path, params: { user: invalid_user_params } + expect(response).to have_http_status(:unprocessable_entity) end it 'ログイン状態にならないこと' do - post user_session_path, params: { user: @invalid_user_params } + post user_session_path, params: { user: invalid_user_params } expect(controller.user_signed_in?).to be false end end end - end