Sha256: 47957d1bcbe6c14c565c560c14ce1fb0a7ef1ef7870c9f19259f5e07d2cfe8d1

Contents?: true

Size: 1.69 KB

Versions: 30

Compression:

Stored size: 1.69 KB

Contents

RSpec.describe 'validate_nilness_of' do
  let(:record) {
    record_class.new
  }

  context 'a model with a nil validation' do
    let(:attribute) {
      :nil_thing
    }

    let(:record_class) {
      # capture attribute for Class.new scope
      attribute = self.attribute

      Class.new do
        include ActiveModel::Validations

        #
        # Attributes
        #

        attr_accessor attribute

        #
        # Validations
        #

        validates attribute,
                  nil: true
      end
    }

    it 'accepts' do
      expect(record).to validate_nilness_of(attribute)
    end

    it 'provides correct error message when negated' do
      expect {
        expect(record).not_to validate_nilness_of(attribute)
      }.to raise_error(
               RSpec::Expectations::ExpectationNotMetError,
               "Expected errors not to include 'must be nil' when #{attribute} is set"
           )
    end
  end

  context 'a model without a nil validation' do
    let(:attribute) {
      :non_nil_thing
    }

    let(:record_class) {
      # capture attribute for Class.new scope
      attribute = self.attribute

      Class.new do
        include ActiveModel::Validations

        #
        # Attributes
        #

        attr_accessor attribute
      end
    }

    it 'rejects' do
      expect(record).not_to validate_nilness_of(attribute)
    end

    it 'provides the correct failure message' do
      expect {
        expect(record).to validate_nilness_of(attribute)
      }.to raise_error(
               RSpec::Expectations::ExpectationNotMetError,
               "Expected errors to include 'must be nil' when #{attribute} is set to an arbitrary string"
           )
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
metasploit-model-5.0.2 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-5.0.1 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-5.0.0 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-4.0.6 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-4.0.5 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-4.0.4 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-4.0.3 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-4.0.2 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-4.0.1 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-4.0.0 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-3.1.4 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-3.1.3 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-3.1.2 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-3.1.1 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-3.1.0 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-3.0.0 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-2.0.4 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-2.0.3 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-2.0.2 spec/matchers/validate_nilness_of_spec.rb
metasploit-model-2.0.1 spec/matchers/validate_nilness_of_spec.rb