spec/acfs/model/attributes_spec.rb in acfs-0.32.1.1.b274 vs spec/acfs/model/attributes_spec.rb in acfs-0.32.1.1.b275

- old
+ new

@@ -40,22 +40,45 @@ describe '#write_attributes' do before do model.attribute :name, :string, default: 'John' model.attribute :age, :integer, default: 25 end + let(:args) { [params] } + let(:params){ {name: 'James'} } let(:m) { model.new } + let(:action) { lambda{ m.write_attributes *args } } + subject { action } - it 'should update attributes' do - m.write_attributes name: 'James' + it 'should update attributes' do + should change(m, :attributes) + .from({'name' => 'John', 'age' => 25}) + .to({'name' => 'James', 'age' => 25}) + end - expect(m.attributes).to be == { name: 'James', age: 25 }.stringify_keys + context 'without non-hash params' do + let(:params) { 'James' } + + it { should_not change(m, :attributes) } + its(:call) { should eq false } end - it 'should do nothing on non-array types' do - ret = m.write_attributes 'James' + context 'with unknown attributes' do + let(:params) { {name: 'James', born_at: Time.now} } - expect(ret).to be false - expect(m.attributes).to be == { name: 'John', age: 25 }.stringify_keys + it { should_not raise_error } + + it 'should update known attributes' do + should change(m, :attributes) + .from({'name' => 'John', 'age' => 25}) + .to({'name' => 'James', 'age' => 25}) + end + + context 'with unknown: :raise option' do + let(:args) { [params, {unknown: :raise}] } + + it { should raise_error(ArgumentError, /unknown attribute/i) } + it { expect{ subject.call rescue true }.to_not change(m, :attributes) } + end end end describe '#_getter_' do before { model.attribute :name, :string, default: 'John' }