Sha256: ae74e79c7647757751f8f2615d5a286546a2d49d74961e75c691ce6582dc1d6d

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

require 'unit_spec_helper'

describe Shoulda::Matchers::ActiveModel::ValidateAcceptanceOfMatcher, type: :model do
  context 'a model with an acceptance validation' do
    it 'accepts when the attributes match' do
      expect(record_validating_acceptance).to matcher
    end

    it 'does not overwrite the default message with nil' do
      expect(record_validating_acceptance).to matcher.with_message(nil)
    end
  end

  context 'a model without an acceptance validation' do
    it 'rejects' do
      expect(record_validating_nothing).not_to matcher
    end
  end

  context 'an attribute which must be accepted with a custom message' do
    it 'accepts when the message matches' do
      expect(record_validating_acceptance(message: 'custom')).
        to matcher.with_message(/custom/)
    end

    it 'rejects when the message does not match' do
      expect(record_validating_acceptance(message: 'custom')).
        not_to matcher.with_message(/wrong/)
    end
  end

  def matcher
    validate_acceptance_of(:attr)
  end

  def model_validating_nothing(&block)
    define_active_model_class(:example, accessors: [:attr], &block)
  end

  def record_validating_nothing
    model_validating_nothing.new
  end

  def model_validating_acceptance(options = {})
    model_validating_nothing do
      validates_acceptance_of :attr, options
    end
  end

  def record_validating_acceptance(options = {})
    model_validating_acceptance(options).new
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shoulda-matchers-3.0.1 spec/unit/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb
shoulda-matchers-3.0.0 spec/unit/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb