Sha256: f9fbff7296fdf079e3b199ab585a5f91f9721a49cd2d2dddb1fa0c44a26d0384

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'spec_helper'

describe Hydra::Validations::PresenceValidator do
  let(:validatable) do
    Class.new do
      def self.name
        'Validatable'
      end
      include ActiveModel::Validations
      include Hydra::Validations
      attr_accessor :an_attribute
      validates_presence_of :an_attribute, message: "Can't have blank values"
    end
  end

  context 'validation scenarios' do
    subject { validatable.new }
    [
      { values: [], valid?: false } ,
      { values: [''], valid?: false } ,
      { values: nil, valid?: false },
      { values: '', valid?: false },
      { values: ['work', ''], valid?: false },
      { values: ['work', nil], valid?: false },
      { values: ['work'], valid?: true },
      { values: 'work', valid?: true }
    ].each do |scenario|
      it "will validate #{scenario.fetch(:values).inspect} as valid? == #{scenario.fetch(:valid?)}" do
        subject.an_attribute = scenario.fetch(:values)
        expect(subject.valid?).to eq(scenario.fetch(:valid?))
      end
    end

    it 'should add error message' do
      subject.an_attribute = nil
      subject.valid?
      expect(subject.errors[:an_attribute]).to eq(["Can't have blank values"])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hydra-validations-0.5.0 spec/validations/presence_validator_spec.rb