Sha256: ee2b833ba759313353fcb60bb4deb2b9175fdaf9140611893b9ca2bfedc229e2

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 KB

Contents

require 'spec_helper'

describe Volt::LengthValidator do
  subject { Volt::LengthValidator.validate(*params) }
  let(:params) { [model, field_name, options] }

  let(:model) { Volt::Model.new name: name }
  let(:field_name) { :name }
  let(:name) { 'John Doe' }

  describe '.validate' do
    describe 'when options is a Fixnum' do
      let(:options) { 5 }

      describe 'when name is "John Doe"' do
        let(:name) { 'John Doe' }
        it { expect(subject).to eq({}) }
      end

      describe 'when name is "John"' do
        let(:name) { 'John' }
        it do
          expect(subject).to eq(name: ['must be at least 5 characters'])
        end
      end
    end

    describe 'when options is a Hash' do
      let(:options) do
        { length: 5, maximum: 10 }
      end

      describe 'when name is "John Doe"' do
        let(:name) { 'John Doe' }
        it { expect(subject).to eq({}) }
      end

      describe 'when name is "John"' do
        let(:name) { 'John' }
        it do
          if RUBY_PLATFORM == 'opal'
            expect(subject).to eq(name: ["must be at least {\"length\"=>5, \"maximum\"=>10} characters"])
          else
            expect(subject).to eq(name: ['must be at least {:length=>5, :maximum=>10} characters'])
          end
        end
      end

      describe 'when name is "Zach Galifianakis"' do
        let(:name) { 'Zach Galifianakis' }
        it do
          if RUBY_PLATFORM == 'opal'
            expect(subject).to eq(name: ["must be less than {\"length\"=>5, \"maximum\"=>10} characters"])
          else
            expect(subject).to eq(name: ['must be less than {:length=>5, :maximum=>10} characters'])
          end
        end
      end
    end

    describe 'when options not a Fixnum or a Hash' do
      let(:options) { 'string' }

      it 'raises an exception' do
        expect { subject }.to raise_error(
          RuntimeError,
          'The arguments to length must be a number or a hash'
        )
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
volt-0.9.3.pre5 spec/models/validators/length_validator_spec.rb
volt-0.9.3.pre4 spec/models/validators/length_validator_spec.rb
volt-0.9.3.pre3 spec/models/validators/length_validator_spec.rb
volt-0.9.3.pre2 spec/models/validators/length_validator_spec.rb
volt-0.9.3.pre1 spec/models/validators/length_validator_spec.rb
volt-0.9.2 spec/models/validators/length_validator_spec.rb