Skip to content

Commit

Permalink
RSpec用rubocop設定,rubocop適用
Browse files Browse the repository at this point in the history
  • Loading branch information
KOH6 committed Oct 8, 2023
1 parent d5a1897 commit 9b52489
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ Hc/RailsSpecificActionName:
Rails/LexicallyScopedActionFilter:
Exclude:
- app/controllers/users/registrations_controller.rb
RSpec/ContextWording:
AllowedPatterns:
- とき$
- カラム$
4 changes: 3 additions & 1 deletion spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

FactoryBot.define do
factory :user do
name { 'test_name' }
Expand All @@ -8,4 +10,4 @@
phone { '123456789' }
birthdate { '2000-01-01' }
end
end
end
16 changes: 8 additions & 8 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +39,7 @@
end
end

context "passwordカラム" do
context 'passwordカラム' do
it '空欄の場合、登録できないこと' do
user.password = ''
expect(user).to be_invalid
Expand All @@ -52,7 +52,7 @@
end
end

context "password_confirmationカラム" do
context 'password_confirmationカラム' do
it '空欄の場合、登録できないこと' do
user.password_confirmation = ''
expect(user).to be_invalid
Expand All @@ -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
Expand Down
27 changes: 14 additions & 13 deletions spec/requests/posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 20 additions & 19 deletions spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 9b52489

Please sign in to comment.