Sha256: 3a4693723e6e8442f0fa44c31d06bdfa720485b59dfebfba1d1a43209ebd73ed

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

require 'spec_helper'

describe Virtus::Attribute::Date do
  it_should_behave_like 'Attribute' do
    let(:attribute_name)         { :created_on }
    let(:attribute_value)        { Date.today }
    let(:attribute_value_other)  { (Date.today+1).to_s }
    let(:attribute_default)      { Date.today-1 }
    let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :created_on } }
  end

  describe '#coerce' do
    let(:attribute) { described_class.new(:bday) }

    let(:year)  { 2011 }
    let(:month) { 4 }
    let(:day)   { 7 }

    subject { attribute.coerce(value) }

    shared_examples_for "a correct date" do
      it          { should be_kind_of(Date) }
      its(:year)  { should eql(year)  }
      its(:month) { should eql(month) }
      its(:day)   { should eql(day)   }
    end

    context 'with a time' do
      it_should_behave_like "a correct date" do
        let(:value) { Time.local(year, month, day) }
      end
    end

    context 'with a date time' do
      it_should_behave_like "a correct date" do
        let(:value) { DateTime.new(year, month, day) }
      end
    end

    context 'with a hash' do
      it_should_behave_like "a correct date" do
        let(:value) do
          { :year => year, :month => month, :day => day }
        end
      end
    end

    context 'with a string' do
      it_should_behave_like "a correct date" do
        let(:value) { "April #{day}th, #{year}" }
      end
    end

    context 'with a non-date value' do
      let(:value) { 'non-date' }
      it { should equal(value) }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
virtus-0.0.10 spec/unit/virtus/attribute/date_spec.rb
virtus-0.0.9 spec/unit/virtus/attribute/date_spec.rb
virtus-0.0.8 spec/unit/virtus/attribute/date_spec.rb
virtus-0.0.7 spec/unit/virtus/attribute/date_spec.rb
virtus-0.0.6 spec/unit/virtus/attribute/date_spec.rb