Sha256: 07d4777ccadb972f0ab4358a67a2dd3a0e8f63bd47e4ea48caeb1e578be9d22a

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

require 'spec_helper'

describe Virtus::Attribute, '#coerce' do
  subject { object.coerce(input) }

  fake(:coercer) { Virtus::Attribute::Coercer }

  let(:object) { described_class.build(String, :coercer => coercer, :strict => strict) }
  let(:input)  { 1 }
  let(:output) { '1' }

  context 'when strict mode is turned off' do
    let(:strict) { false }

    it 'uses coercer to coerce the input value' do
      stub(coercer).call(input) { output }

      expect(subject).to be(output)

      expect(coercer).to have_received.call(input)
    end
  end

  context 'when strict mode is turned on' do
    let(:strict) { true }

    it 'uses coercer to coerce the input value' do
      stub(coercer).call(input) { output }
      stub(coercer).success?(String, output) { true }

      expect(subject).to be(output)

      expect(coercer).to have_received.call(input)
      expect(coercer).to have_received.success?(String, output)
    end

    it 'raises error when input was not coerced' do
      stub(coercer).call(input) { input }
      stub(coercer).success?(String, input) { false }

      expect { subject }.to raise_error(ArgumentError)

      expect(coercer).to have_received.call(input)
      expect(coercer).to have_received.success?(String, input)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
virtus-1.0.0.beta6 spec/unit/virtus/attribute/coerce_spec.rb
virtus-1.0.0.beta5 spec/unit/virtus/attribute/coerce_spec.rb
virtus-1.0.0.beta4 spec/unit/virtus/attribute/coerce_spec.rb
virtus-1.0.0.beta3 spec/unit/virtus/attribute/coerce_spec.rb