Extract and stub PaymentMethodFetcher

This commit is contained in:
Matt-Yorkley
2022-01-20 10:36:12 +00:00
parent ebb69d7f93
commit 8552da2377
4 changed files with 53 additions and 2 deletions

View File

@@ -63,7 +63,7 @@ class SplitCheckoutController < ::BaseController
end
def selected_payment_method
@selected_payment_method ||= @order.payments.order(:created_at).last&.payment_method
@selected_payment_method ||= Checkout::PaymentMethodFetcher.new(@order).call
end
def update_order

View File

@@ -0,0 +1,21 @@
# frozen_string_literal: true
module Checkout
class PaymentMethodFetcher
def initialize(order)
@order = order
end
def call
latest_payment&.payment_method
end
private
attr_reader :order
def latest_payment
order.payments.order(:created_at).last
end
end
end

View File

@@ -196,7 +196,8 @@ describe SplitCheckoutController, type: :controller do
context "when an external payment gateway is used" do
before do
expect(controller).to receive(:selected_payment_method).at_least(:once) { payment_method }
expect(Checkout::PaymentMethodFetcher).
to receive_message_chain(:new, :call) { payment_method }
expect(payment_method).to receive(:external_gateway?) { true }
expect(payment_method).to receive(:external_payment_url) { "https://example.com/pay" }
end

View File

@@ -0,0 +1,29 @@
# frozen_string_literal: true
require 'spec_helper'
describe Checkout::PaymentMethodFetcher do
let!(:order) { create(:completed_order_with_totals) }
let(:payment1) { build(:payment, order: order) }
let(:payment2) { build(:payment, order: order) }
let(:service) { Checkout::PaymentMethodFetcher.new(order) }
describe '#call' do
context "when the order has payments" do
before do
order.payments << payment1
order.payments << payment2
end
it "returns the payment_method of the most recently created payment" do
expect(service.call).to eq payment2.payment_method
end
end
context "when the order has no payments" do
it "returns nil" do
expect(service.call).to be_nil
end
end
end
end