Sha256: cda16c8017ed0b17647e12f2bd36a6f67f286230c51a767e710bee61523da776

Contents?: true

Size: 1.81 KB

Versions: 31

Compression:

Stored size: 1.81 KB

Contents

require 'spec_helper'

describe DerivationValidator do
  subject(:derivation_validator) do
    described_class.new(
        :attributes => attributes
    )
  end

  let(:attribute) do
    :full_name
  end

  let(:attributes) do
    [
        attribute
    ]
  end

  context '#validate_each' do
    subject(:validate_each) do
      derivation_validator.validate_each(record, attribute, value)
    end

    let(:record) do
      record_class.new
    end

    let(:record_class) do
      # capture attribute name in local so it is accessible in Class block scope.
      attribute = self.attribute

      Class.new do
        include ActiveModel::Validations

        validates attribute,
                  :derivation => true
      end
    end

    let(:value) do
      nil
    end

    context 'without derived_<attribute>' do
      it 'should raise error' do
        expect {
          validate_each
        }.to raise_error(NoMethodError)
      end
    end

    context 'with derived_<attribute>' do
      let(:derived_value) do
        'derived_value'
      end

      before(:each) do
        # capture for record scope
        derived_value = self.derived_value

        record.define_singleton_method("derived_#{attribute}") do
          derived_value
        end
      end

      context 'with value matches derived_<attribute>' do
        let(:value) do
          derived_value
        end

        it 'should not record any errors' do
          validate_each

          record.errors.should be_empty
        end
      end

      context 'without value matches derived_<attribute>' do
        let(:value) do
          'non_derived_value'
        end

        it 'should record error on attribute' do
          validate_each

          record.errors[attribute].should include('must match its derivation')
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
metasploit-model-0.28.0 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.28.0-java spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.28.0.pre.engine.pre.requires spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.4 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.4-java spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.3 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.3-java spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.2 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.2-java spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.1 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.1-java spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.0 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.0-java spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.27.0.pre.dep.pre.railties spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.26.1-java spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.26.1 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.25.7 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.25.7-java spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.25.6 spec/app/validators/derivation_validator_spec.rb
metasploit-model-0.25.6-java spec/app/validators/derivation_validator_spec.rb