spec/lib/azeroth/request_handler/update_spec.rb in azeroth-0.6.5 vs spec/lib/azeroth/request_handler/update_spec.rb in azeroth-0.7.0

- old
+ new

@@ -28,9 +28,100 @@ .from(document.name) .to('New Name') end end + context 'with before_save block option' do + it_behaves_like 'a request handler' do + let(:block) do + value = 10 + proc do + document.reference = "X-MAGIC-#{value}" + end + end + + let(:options_hash) do + { + before_save: block + } + end + + let(:expected_resource) { document } + + let(:extra_params) do + { + id: document.id, + document: { + name: 'New Name' + } + } + end + + let(:expected_json) do + { + 'name' => 'New Name', + 'reference' => 'X-MAGIC-10' + } + end + + it 'updates the values given by request' do + expect { handler.process } + .to change { document.reload.name } + .from(document.name) + .to('New Name') + end + + it 'updates the values done by before_save' do + expect { handler.process } + .to change { document.reload.reference } + .from(nil) + .to('X-MAGIC-10') + end + end + end + + context 'with before_save symbol option' do + it_behaves_like 'a request handler' do + let(:options_hash) do + { + before_save: :add_magic_reference + } + end + + let(:expected_resource) { document } + + let(:extra_params) do + { + id: document.id, + document: { + name: 'New Name' + } + } + end + + let(:expected_json) do + { + 'name' => 'New Name', + 'reference' => 'X-MAGIC-15' + } + end + + it 'updates the values given by request' do + expect { handler.process } + .to change { document.reload.name } + .from(document.name) + .to('New Name') + end + + it 'updates the values done by before_save' do + expect { handler.process } + .to change { document.reload.reference } + .from(nil) + .to('X-MAGIC-15') + end + end + end + context 'when payload is invalid' do it_behaves_like 'a request handler', status: :unprocessable_entity do let(:expected_resource) { document } let(:extra_params) do