Sha256: 1f7143412e1b5901b2ea733b0a8c97fa4f24d8aa8f6dce7ecb5990d7f55de364

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

require 'spec_helper'
require 'volt/models'

class ExampleModelWithField < Volt::Model
  field :name
  field :value, Numeric
end

class ExampleModelWithField2 < ExampleModelWithField
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

    expect(model.name).to eq(nil)
    model.name = 'jimmy'
    expect(model.name).to eq('jimmy')

    expect(model.value).to eq(nil)
    model.value = '20.5'

    # Should be cast to float
    expect(model.value).to eq(20.5)
  end

  it 'should raise an error when an invalid cast type is provided' do
    expect do
      ExampleModelWithField2.field :awesome, Range
    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

  it 'should track the fields on the model class' do
    expect(ExampleModelWithField.fields_data).to eq({name:[nil, {}], value:[[Numeric, NilClass], {}]})
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
volt-0.9.6 spec/models/field_helpers_spec.rb
volt-0.9.6.pre3 spec/models/field_helpers_spec.rb
volt-0.9.6.pre2 spec/models/field_helpers_spec.rb
volt-0.9.6.pre1 spec/models/field_helpers_spec.rb