Sha256: 9ed381eb87ea549c492b5f2a82430af9f74232535c2809cc9e66eeac5ad2fa7c

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

require 'test_helper'

class ValidationMatcherTest < Minitest::Test
  def setup
    @validator = ValidationMatcher.new :attr, :type
  end

  test 'returns false if validation type not exists' do
    subject = create_subject :attr, :another_type

    assert !@validator.matches?(subject)
  end

  test 'returns true if validation type exists' do
    subject = create_subject :attr, :type

    assert @validator.matches? subject
  end

  test 'set failure message if validation type not exists' do
    subject = create_subject :attr, :another_type

    @validator.matches? subject

    assert_match 'no type validator for attr', @validator.failure_message
  end

  test 'set negative failure message if validation type exists' do
    subject = create_subject :attr, :type

    @validator.matches? subject

    assert_match 'type validator for attr', @validator.negative_failure_message
  end

  test 'returns true if validation type exists with on option' do
    subject = create_subject :attr, :type, on: [ :create ]

    assert @validator.on(:create).matches? subject
  end

  test 'returns false if validation type not exists with on option' do
    subject = create_subject :attr, :type, on: [ :create ]

    assert !@validator.on(:update).matches?(subject)
  end

  test 'returns true if validation type exists with with_message option' do
    subject = create_subject :attr, :type, message: 'error'

    assert @validator.with_message('error').matches? subject
  end

  test 'returns false if validation type not exists with with_message option' do
    subject = create_subject :attr, :type, message: 'different error'

    assert !@validator.with_message('error').matches?(subject)
  end

  private

  def create_subject attr, type, options={}
    arg = MiniTest::Mock.new
    arg.expect :kind, type
    arg.expect :options, options

    subject = MiniTest::Mock.new
    subject.expect :is_a?, true, [Class]
    subject.expect :validators_on, [arg], [attr]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minitest-activemodel-1.1.0 test/cases/validation_matcher_test.rb