spec/unit/virtus/attribute/embedded_value/coerce_spec.rb in virtus-1.0.0.rc2 vs spec/unit/virtus/attribute/embedded_value/coerce_spec.rb in virtus-1.0.0

- old
+ new

@@ -1,11 +1,12 @@ require 'spec_helper' describe Virtus::Attribute::EmbeddedValue, '#coerce' do subject { object.coerce(input) } - let(:object) { described_class.build(model) } + let(:object) { described_class.build(model, options) } + let(:options) { {} } context 'when primitive is OpenStruct' do let(:model) { OpenStruct } context 'when input is an attribute hash' do @@ -50,8 +51,27 @@ context 'when input is a model instance' do let(:input) { model.new('Piotr', 30) } it { should be(input) } + end + end + + context 'when :strict mode is enabled' do + let(:model) { Struct.new(:name) } + let(:options) { { :strict => true } } + + context 'when input is coercible' do + let(:input) { ['Piotr'] } + + it { should eql(model.new('Piotr')) } + end + + context 'when input is not coercible' do + let(:input) { nil } + + it 'raises error' do + expect { subject }.to raise_error(Virtus::CoercionError) + end end end end