Sha256: 88b0879f7992beb41226762f09ef6cfa7b566b55491de737afc3e608dadbaa73

Contents?: true

Size: 1.19 KB

Versions: 11

Compression:

Stored size: 1.19 KB

Contents

require 'spec_helper'

describe Virtus::Coercion::Object, '.to_array' do
  subject { object.to_array(value) }

  let(:object)  { described_class }
  let(:value)   { Object.new      }
  let(:coerced) { [ value ]       }

  context 'when the value responds to #to_ary' do
    before do
      value.should_receive(:to_ary).with().and_return(coerced)
    end

    it { should be(coerced) }

    it 'does not call #to_a if #to_ary is available' do
      value.should_not_receive(:to_a)
      subject
    end
  end

  context 'when the value responds to #to_a but not #to_ary' do
    before do
      value.should_receive(:to_a).with().and_return(coerced)
    end

    it { should be(coerced) }
  end

  context 'when the value does not respond to #to_ary or #to_a' do
    it { should be_instance_of(Array) }

    it { should == coerced }
  end

  context 'when the value returns nil from #to_ary' do
    before do
      value.should_receive(:to_ary).with().and_return(nil)
    end

    it 'calls #to_a as a fallback' do
      value.should_receive(:to_a).with().and_return(coerced)
      should be(coerced)
    end

    it 'wraps the value in an Array if #to_a is not available' do
      should == coerced
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
virtus-0.5.5 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.5.4 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.5.3 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/virtus-0.5.2/spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.5.2 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.5.1 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.5.0 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.4.2 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.4.1 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.4.0 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
virtus-0.3.0 spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb