= ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end 例外が発生したら? 例外が発生したら? 分岐 例外が発生したら? 例外が発生したら? ここだけ握りつぶしてる
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end ブロック渡しがシンプルで
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end doubleの宣言と一緒に メソッド定義できるのが
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end doubleの宣言と一緒にメソッド定義できるのが
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end もともとの挙動を使いたいときに使えて
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end
subject { foo.complex_method } context 'リクエストが成功して新しいレコ⑲ド作成に成功した場合' do let(:object) { double(:object, new?: true, to_h: { a: :b}) } let(:response_handler) { double(:response_handler, do_something: object) } before do allow(foo).to receive(:request) { { bar: 1} } allow(ResponseHandler).to receive(:new) { response_handler } allow(AModel).to receive(:create!).and_call_original allow(AModel).to receive(:update!).and_call_original end it 'レコ⑲ドが1件できている' do subject expect(AModel.count).to eq 1 expect(foo).to have_received(:request).with(...).once expect(ResponseHandler).to have_received(:new).with(bar: 1).once expect(response_handler).to have_received(:do_something).once expect(AModel).to have_received(:create!).with({ a: :b }).once expect(AModel).not_to have_received(:update!) end end end end class Foo def complex_method response = request(params, headers) object = ResponseHandler.new(response).do_something begin if object.new? AModel.create!(object.to_h) else AModel.update!(object.to_h) end rescue => e logger.error(e) end end end