Sha256: d355eb990b7cebf9fa8a26e656bc0eafa68de18d282af75ff48e5bc35c53af46

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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, 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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
volt-0.9.5.pre4 spec/models/field_helpers_spec.rb