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
Conversation
spec/models/task_spec.rb
Outdated
|
||
RSpec.describe Task, type: :model do | ||
describe '#save' do | ||
let(:task) { described_class.new(name: 'hoge') } |
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.
factorybotを使用すると後々のテストが楽になると思いました。
https://qiita.com/Ushinji/items/522ed01c9c14b680222c
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.
factorybot を使用するように修正しました。
8afae2c
expect(task.errors.count).to eq(0) | ||
end | ||
|
||
context 'nameへの入力がない' do |
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.
境界値テストとして1文字の場合もテストした方が良さそうです。
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.
context で括ってない正常系のテストを1文字のケースとして変更しました。
8afae2c
end | ||
end | ||
|
||
context 'nameへ21文字以上の入力があると' do |
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.
境界値テストとして20文字の場合もテストした方が良さそうです。
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)
こちらも上記に合わせてケースを追加しています。
let(:task) { described_class.new(name: 'hoge') } | ||
|
||
it 'creates records in task' do | ||
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.
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.
修正ありがとうございます。
可読性など見やすさに重きを置いても良いのかな
おっしゃる通りだと思います。
過度なリファクタリングは避けた方が良さそうです。
spec/models/task_spec.rb
Outdated
end | ||
|
||
context 'nameへ21文字以上の入力があると' do | ||
let(:task) { described_class.new(name: '12345678901234567890a') } |
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.
nameは実際のデータを入力するよりも
(name: 'a' * 21)
とした方がコンパクト且つ視覚的に分かりやすいですー
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)
こちらも合わせて修正しています。
@masahikokawai また2点お伝えしたいことがあります。
原因:最近
|
・境界値のケースを追加 ・it内のtask.saveの記述の修正
0cecca9
to
69ef535
Compare
statusのspecも追加しました。 |
情報ありがとうございます! |
・model specにも追加
69ef535
to
ac674b2
Compare
spec/models/task_spec.rb
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
こちらは、nameに対するテストですので、
letにはnameのみ設定した方が良さそうです。(statusも設定するとテストが読みづらい気がします)
もしよろしければ、参考にしてください
https://github.com/Fablic/training/blob/hoooori/task_app/spec/models/task_spec.rb
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.
テストのケースきちんと書いていて素晴らしいです!!
参考にしました!
statusのケースを分けるように修正しています。
b8fdad9
@masahikokawai 1点コメントしましたので確認をお願いします。 |
@masahikokawai |
ステップ11: バリデーションを設定してみよう
■桁数超過
■必須
デザインは何もしてないです。
(field_with_errors を自動で差し込んでくれるのに対して)