From fd5e0fd60f17c36009c8571c7f0320ce47ec29e0 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 7 May 2021 08:35:54 +0200 Subject: [PATCH] Properly mock validator and extend tests Tests were passing but not due to the expected reason. While at it, we extended the assertions to be more precise. --- spec/controllers/spree/orders_controller_spec.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index fc7a0b32798..6e1ecd11e59 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -106,7 +106,7 @@ before do allow_any_instance_of(Stripe::PaymentIntentValidator) .to receive(:call) - .with(payment_intent, anything) + .with(payment_intent, kind_of(String)) .and_return(payment_intent) end @@ -122,19 +122,19 @@ context "with an invalid payment intent" do let(:payment_intent) { "invalid" } - let(:result) { instance_double(ProcessPaymentIntent::Result) } before do allow_any_instance_of(Stripe::PaymentIntentValidator) .to receive(:call) - .with(payment_intent, anything) - .and_return(result) + .with(payment_intent, kind_of(String)) + .and_raise(Stripe::StripeError, "error message") end it "does not complete the payment" do get :show, params: { id: order.number, payment_intent: payment_intent } expect(response).to be_success + expect(flash[:error]).to eq("#{I18n.t("payment_could_not_process")}. error message") payment.reload expect(payment.cvv_response_message).to eq("https://stripe.com/redirect") expect(payment.state).to eq("pending") @@ -148,12 +148,18 @@ before do allow(payment).to receive(:response_code).and_return("invalid") allow(OrderPaymentFinder).to receive(:new).with(order).and_return(finder) + + allow_any_instance_of(Stripe::PaymentIntentValidator) + .to receive(:call) + .with(payment_intent, kind_of(String)) + .and_return(payment_intent) end it "does not complete the payment" do get :show, params: { id: order.number, payment_intent: payment_intent } expect(response).to be_success + expect(flash[:error]).to eq("#{I18n.t("payment_could_not_process")}. ") payment.reload expect(payment.cvv_response_message).to eq("https://stripe.com/redirect") expect(payment.state).to eq("pending")