Sha256: 01893ac4e1a07e22f45742e3d1993c93f7b37a539fd0ac4cb8164c2f049bd368

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

require 'spec_helper'

describe ":default options" do
  
  describe "string attribute" do

    let(:model_klass) do
      _person = Person.dup
      _person.instance_eval do
        include AttributeEnums::ActiveRecord
        attribute_enums :gender, in: [:male, :female], default: :male
      end
      _person
    end

    subject { model_klass.new }

    it do
      subject.gender = nil
      subject.valid?
      subject.gender.should == :male
    end

    it do
      subject.gender = :female
      subject.valid?
      subject.gender.should == :female
    end

    it do
      my_person = Person.dup
      lambda do
        my_person.instance_eval do
          include AttributeEnums::ActiveRecord
          attribute_enums :gender, in: [:male, :female], default: :unknow
        end
      end.should raise_error(Exception, /default value not match/)
    end

    it do
      my_person = Person.dup
      lambda do
        my_person.instance_eval do
          include AttributeEnums::ActiveRecord
          attribute_enums :gender, in: [:male, :female], default: 'male'
        end
      end.should_not raise_error(Exception, /default value not match/)
    end
  end

  describe "boolean attribute" do

    let(:model_klass) do
      _person = Person.dup
      _person.instance_eval do
        include AttributeEnums::ActiveRecord
        attribute_enums :enable, boolean: true, default: true
      end
      _person
    end

    subject { model_klass.new }

    it do
      subject.enable = nil
      subject.valid?
      subject.enable.should == true
    end

    it do
      subject.enable = false
      subject.valid?
      subject.enable.should == false
    end
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attribute_enums-0.2.2 spec/common_and_active_record/default_options_spec.rb
attribute_enums-0.2.1 spec/common_and_active_record/default_options_spec.rb
attribute_enums-0.2.0 spec/common_and_active_record/default_options_spec.rb