Rails: Specs for Inclusion Validations
assignment.rb
class Assignment < ActiveRecord::Base
STATUS_OPTIONS = ['Not Started', 'In Progress', 'Completed']
validates :status, presence: true, inclusion: STATUS_OPTIONS
end
assignment_spec.rb
describe 'status' do
context 'valid entry' do
['Not Started', 'In Progress', 'Completed'].each do |valid_status|
it "is valid with 'Not Started', 'In Progress', 'Completed'" do
expect(build(:assignment, status: valid_status)).to be_valid
end
end
end
context 'invalid entry' do
it "is invalid without 'Not Started', 'In Progress', 'Completed'" do
expect(build(:assignment, status: 'a')).to_not be_valid
end
end
end
Written by Efren Aguirre
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#