File: required_attributes_spec.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (25 lines) | stat: -rw-r--r-- 698 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require 'spec_helper'

describe 'Using required attributes' do
  before do
    module Examples
      class User
        include Virtus.model(:strict => true)

        attribute :name, String
        attribute :age,  Integer, :required => false
      end
    end
  end

  it 'raises coercion error when required attribute is nil' do
    expect { Examples::User.new(:name => nil) }.to raise_error(Virtus::CoercionError, "Failed to coerce attribute `name' from nil into String")
  end

  it 'does not raise coercion error when not required attribute is nil' do
    user = Examples::User.new(:name => 'Jane', :age => nil)

    expect(user.name).to eql('Jane')
    expect(user.age).to be(nil)
  end
end