require "rails_helper" RSpec.describe Billing::Stripe::EnsureCustomer do let(:user) { User.create!(email: "ensure@test.it", name: "U", password: "password123", role: "coach") } let(:club) do Club.create!(name: "Ensure Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") end before do load Rails.root.join("db/seeds/plans.rb") Billing::AssignPlan.call(club: club, plan_slug: "free") club.subscription.update!(stripe_customer_id: "cus_stale_test_mode") end it "ricrea il customer se l'id salvato non esiste su Stripe" do err = ::Stripe::InvalidRequestError.new("No such customer", 404, code: "resource_missing") allow(Stripe::Customer).to receive(:retrieve).with("cus_stale_test_mode").and_raise(err) allow(Stripe::Customer).to receive(:create).and_return( double(id: "cus_live_new", email: user.email) ) allow(Stripe::Customer).to receive(:update) id = described_class.call(club: club, user: user) expect(id).to eq("cus_live_new") expect(club.subscription.reload.stripe_customer_id).to eq("cus_live_new") expect(club.subscription.stripe_subscription_id).to be_nil end end