Skip to content

Commit

Permalink
statusのバリデーションチェックを追加
Browse files Browse the repository at this point in the history
・model specにも追加
  • Loading branch information
masahiko kawai committed Jun 3, 2019
1 parent 8afae2c commit 69ef535
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ class Task < ApplicationRecord
enum status: { waiting: 1, work_in_progress: 2, completed: 3 }

validates :name, presence: true, length: { maximum: 20 }
validates :status, presence: true,
inclusion: { in: self.statuses.keys }
end
22 changes: 20 additions & 2 deletions spec/models/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe Task, type: :model do
describe '#save' do
let(:task) { build(:task, name: 'a' * 1) }
let(:task) { build(:task, name: 'a' * 1, status: 1) }

before do
task.save
Expand All @@ -25,7 +25,7 @@
end

context 'nameへ20文字の入力があると' do
let(:task) { build(:task, name: 'a' * 20) }
let(:task) { build(:task, name: 'a' * 20, status: 2) }

it 'creates records in task' do
expect(Task.count).to eq(1)
Expand All @@ -41,5 +41,23 @@
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

0 comments on commit 69ef535

Please sign in to comment.