-
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 1 commit
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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Task, type: :model do | ||
describe '#save' do | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 7行目の下に 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)
ちなみに個人的にspecは何もかもDRYにする事を目指すより 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. 修正ありがとうございます。
おっしゃる通りだと思います。 |
||
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) { described_class.new(name: nil) } | ||
|
||
it '必須のエラーメッセージが出ること' do | ||
task.save | ||
|
||
expect(Task.count).to eq(0) | ||
expect(task.errors[:name]).to include('を入力してください') | ||
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) { described_class.new(name: '12345678901234567890a') } | ||
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. ですね、視覚的にもわかりやすいですね! |
||
|
||
it '桁数超過のエラーメッセージが出ること' do | ||
task.save | ||
|
||
expect(Task.count).to eq(0) | ||
expect(task.errors[:name]).to include('は20文字以内で入力してください') | ||
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.
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