spec/mongoid/enum_spec.rb in mongoid-enum-i18n-0.2.5 vs spec/mongoid/enum_spec.rb in mongoid-enum-i18n-0.2.9

- old
+ new

@@ -3,11 +3,14 @@ class User include Mongoid::Document include Mongoid::Enum enum :status, [:awaiting_approval, :approved, :banned] - enum :roles, [:author, :editor, :admin], multiple: true, default: [], required: false + enum :roles, [:author, :editor, :admin], + default: [], + multiple: true, + required: false end describe Mongoid::Enum do let(:klass) { User } let(:instance) { User.new } @@ -50,18 +53,46 @@ end end end end + describe "'required' option" do + context 'when true' do + let(:instance) { User.new status: nil } + it 'is not valid with nil value' do + expect(instance).to_not be_valid + end + end + + context 'when false' do + let(:instance) { User.new roles: nil } + it 'is valid with nil value' do + expect(instance).to be_valid + end + end + end + describe 'constant' do it 'is set to the values' do expect(klass::STATUS).to eq values end end describe 'accessors'do context 'when singular' do + describe 'setter' do + it 'accepts strings' do + instance.status = 'banned' + expect(instance.status).to eq :banned + end + + it 'accepts symbols' do + instance.status = :banned + expect(instance.status).to eq :banned + end + end + describe '{{value}}!' do it 'sets the value' do instance.save instance.banned! expect(instance.status).to eq :banned @@ -85,10 +116,36 @@ end end end context 'when multiple' do + describe 'setter' do + it 'accepts strings' do + instance.roles = 'author' + expect(instance.roles).to eq [:author] + end + + it 'accepts symbols' do + instance.roles << :author + expect(instance.roles).to eq [:author] + end + + it 'accepts arrays of strings' do + instance.roles = %w( author editor ) + instance.save + puts instance.errors.full_messages + instance.reload + expect(instance.roles).to include(:author) + expect(instance.roles).to include(:editor) + end + + it 'accepts arrays of symbols' do + instance.roles = [:author, :editor] + expect(instance.roles).to include(:author) + expect(instance.roles).to include(:editor) + end + end describe '{{value}}!' do context 'when field is nil' do it 'creates an array containing the value' do instance.roles = nil instance.save @@ -181,10 +238,10 @@ it 'should have a helper to translate the enum' do expect(instance.status_i18n).to eq('Awaiting Approval') end it 'should have a helper to translate the values' do - expect(instance.status_values) + expect(User.status_values) .to eq([['Awaiting Approval', :awaiting_approval]]) end end end