spec/models/field_helpers_spec.rb in volt-0.9.3 vs spec/models/field_helpers_spec.rb in volt-0.9.4.pre1
- old
+ new
@@ -5,12 +5,12 @@
field :name
field :value, Numeric
end
describe 'field helpers' do
+ let(:model) { ExampleModelWithField.new }
it 'should allow a user to setup a field that can be written to and read' do
- model = ExampleModelWithField.new
expect(model.name).to eq(nil)
model.name = 'jimmy'
expect(model.name).to eq('jimmy')
@@ -23,7 +23,23 @@
it 'should raise an error when an invalid cast type is provided' do
expect do
ExampleModelWithField.field :awesome, Array
end.to raise_error(FieldHelpers::InvalidFieldClass)
+ end
+
+ it 'should convert numeric strings to Fixnum when Fixnum is specified as a type restriction' do
+ model.value = '22'
+ expect(model.value).to eq(22)
+ end
+
+ it 'should not convert non-numeric strings (and have a validation error)' do
+ # use a buffer, so we can put the model into an invalid state
+ buf = model.buffer
+ buf.value = 'cats'
+ expect(buf.value).to eq('cats')
+
+ buf.validate!.fail do |error|
+ expect(error).to eq({})
+ end
end
end