Last Updated: February 25, 2016
·
1.518K
· countxyz

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