spec/acfs/model/persistance_spec.rb in acfs-0.20.0 vs spec/acfs/model/persistance_spec.rb in acfs-0.21.0.b185

- old
+ new

@@ -1,11 +1,11 @@ require 'spec_helper' describe Acfs::Model::Persistence do let(:model_class) { MyUser } before do - @get_stub = stub_request(:get, 'http://users.example.org/users/1').to_return response({ id: 1, name: "Anon", age: 12 }) + @get_stub = stub_request(:get, 'http://users.example.org/users/1').to_return response({ id: 1, name: 'Anon', age: 12 }) @patch_stub = stub_request(:put, 'http://users.example.org/users/1') .with(body: '{"id":1,"name":"Idefix","age":12}') .to_return response({ id: 1, name: 'Idefix', age: 12 }) @@ -69,10 +69,24 @@ it { expect(model).to be_persisted } it { expect(model).to_not be_new } end end + context 'unloaded model' do + let!(:model) { model_class.find 1 } + + describe '#update_attributes' do + subject { -> { model.update_attributes name: 'John' } } + it { expect{ subject.call }.to raise_error Acfs::ResourceNotLoaded } + end + + describe '#update_attributes!' do + subject { -> { model.update_attributes! name: 'John' } } + it { expect{ subject.call }.to raise_error Acfs::ResourceNotLoaded } + end + end + context 'loaded model' do context 'without changes' do let(:model) { model_class.find 1 } before { model; Acfs.run } @@ -100,10 +114,50 @@ it 'should be frozen after DELETE' do model.delete! expect(model).to be_frozen end end + + describe '#update_atributes!' do + let(:model) { model_class.find 1 } + before { model; Acfs.run } + + it 'should set attributes' do + model.update_attributes name: 'Idefix' + expect(model.attributes.symbolize_keys).to eq id: 1, name: 'Idefix', age: 12 + end + + it 'should save resource' do + expect(model).to receive(:save).with({}) + model.update_attributes name: 'Idefix' + end + + it 'should pass second hash to save' do + expect(model).to receive(:save).with({ bla: 'blub' }) + model.update_attributes({ name: 'Idefix' }, { bla: 'blub' }) + end + end + + describe '#update_atributes' do + let(:model) { model_class.find 1 } + before { model; Acfs.run } + + it 'should set attributes' do + model.update_attributes! name: 'Idefix' + expect(model.attributes.symbolize_keys).to eq id: 1, name: 'Idefix', age: 12 + end + + it 'should save resource' do + expect(model).to receive(:save!).with({}) + model.update_attributes! name: 'Idefix' + end + + it 'should pass second hash to save' do + expect(model).to receive(:save!).with({ bla: 'blub' }) + model.update_attributes!({ name: 'Idefix' }, { bla: 'blub' }) + end + end end describe '.create!' do context 'with valid data' do let(:data) { { name: 'Idefix', age: 12 } } @@ -123,11 +177,11 @@ context 'with invalid data' do let(:data) { { age: 12 } } it 'should raise an error' do expect { model_class.create! data }.to raise_error ::Acfs::InvalidResource do |error| - expect(error.errors).to be == { name: ['required'] }.stringify_keys + expect(error.errors).to be == { name: %w(required) }.stringify_keys end end end end @@ -155,10 +209,10 @@ expect(model).to_not be_persisted end it 'should contain error hash' do model = model_class.create data - expect(model.errors.to_hash).to be == { name: [ "required" ]}.stringify_keys + expect(model.errors.to_hash).to be == { name: %w(required) }.stringify_keys end end end end