-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tasks.nameに必須と桁数のバリデーション処理を入れる #339
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ChangeColumnToTask < ActiveRecord::Migration[5.2] | ||
def change | ||
change_column :tasks, :name, :string, limit: 20 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Task, type: :model do | ||
describe '#save' do | ||
let(:task) { build(:task, name: 'a' * 1, status: 1) } | ||
|
||
before do | ||
task.save | ||
end | ||
|
||
it 'creates records in task' do | ||
expect(Task.count).to eq(1) | ||
expect(task.errors.count).to eq(0) | ||
end | ||
|
||
context 'nameへの入力がない' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 境界値テストとして1文字の場合もテストした方が良さそうです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. context で括ってない正常系のテストを1文字のケースとして変更しました。 |
||
let(:task) { build(:task, name: nil) } | ||
|
||
it '必須のエラーメッセージが出ること' do | ||
expect(Task.count).to eq(0) | ||
expect(task.errors[:name]).to include('を入力してください') | ||
end | ||
end | ||
|
||
context 'nameへ20文字の入力があると' do | ||
let(:task) { build(:task, name: 'a' * 20, status: 2) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらは、nameに対するテストですので、 もしよろしければ、参考にしてください There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. テストのケースきちんと書いていて素晴らしいです!! statusのケースを分けるように修正しています。 |
||
|
||
it 'creates records in task' do | ||
expect(Task.count).to eq(1) | ||
expect(task.errors.count).to eq(0) | ||
end | ||
end | ||
|
||
context 'nameへ21文字以上の入力があると' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 境界値テストとして20文字の場合もテストした方が良さそうです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #339 (comment) |
||
let(:task) { build(:task, name: 'a' * 21) } | ||
|
||
it '桁数超過のエラーメッセージが出ること' do | ||
expect(Task.count).to eq(0) | ||
expect(task.errors[:name]).to include('は20文字以内で入力してください') | ||
end | ||
end | ||
|
||
context 'statusへ既定値以外(-1)の入力があると' do | ||
it 'statusにマイナスの値で永続化できないこと' do | ||
expect { task.assign_attributes(status: -1) }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context 'statusへ既定値以外(0)の入力があると' do | ||
it 'statusに0の値で永続化できないこと' do | ||
expect { task.assign_attributes(status: 0) }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context 'statusへ既定値以外(4)の入力があると' do | ||
it 'statusに4の値で永続化できないこと' do | ||
expect { task.assign_attributes(status: 4) }.to raise_error(ArgumentError) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
7行目の下に
before { task.save }
を定義してsave処理を共通化すれば、各テストからtask.saveを削除できるのでコンパクト化できそうです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#339 (comment)
こちらも合わせて修正しました。
ちなみに個人的にspecは何もかもDRYにする事を目指すより
可読性など見やすさに重きを置いても良いのかなと思ってます。(あくまで個人的にです・・)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正ありがとうございます。
おっしゃる通りだと思います。
過度なリファクタリングは避けた方が良さそうです。