Sha256: 5f36bc205f747d3955da3fc20d8c50a5a1884713c7fbdd1446eebda3daff451a

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require 'spec_helper'

describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::OnlyIntegerMatcher do
  it_behaves_like 'a numerical submatcher' do
    subject { described_class.new(:attr) }
  end

  context 'given an attribute that only allows integer values' do
    it 'matches' do
      validating_only_integer.should new_matcher
    end

    it 'allows integer types' do
      new_matcher.allowed_types.should eq 'integer'
    end

    it 'returns itself when given a message' do
      matcher = new_matcher
      matcher.with_message('some message').should eq matcher
    end
  end

  context 'given an attribute that only allows integer values with a custom validation message' do
    it 'only accepts integer values for that attribute with that message' do
      validating_only_integer(:message => 'custom').should new_matcher.with_message(/custom/)
    end

    it 'rejects integer values for that attribute with another message' do
      validating_only_integer(:message => 'custom').should_not new_matcher.with_message(/wrong/)
    end
  end

  context 'when the model does not have an only_integer validation' do
    it 'does not match' do
      define_model(:example, :attr => :string).new.should_not new_matcher
    end

    it 'fails with the ActiveRecord :not_an_integer message' do
      matcher = new_matcher

      matcher.matches?(define_model(:example, :attr => :string).new)

      matcher.failure_message.should include 'Expected errors to include "must be an integer"'
    end
  end

  def new_matcher
    described_class.new(:attr)
  end

  def validating_only_integer(options = {})
    define_model :example, :attr => :string do
      validates_numericality_of :attr, { :only_integer => true }.merge(options)
    end.new
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shoulda-matchers-2.5.0 spec/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher_spec.rb