title: How to test Stripe's invoice_pdf property when it keeps changing?
body: <p>In my Rails 6 app I have a very simple controller that displays download links to a user's Stripe invoice PDFs:</p>
  <pre><code>class ReceiptsController &lt; ApplicationController
  
    before_action :signed_in_user
  
    def index
      receipts = current_account.receipts
    end
  
    def show
      receipt = current_account.receipts.find(params[:id])
      stripe_invoice = Stripe::Invoice.retrieve(receipt.stripe_invoice_id)
      redirect_to stripe_invoice.invoice_pdf
    end
  
  end
  </code></pre>
  <p>Since Stripe doesn't provide <em>permanent</em> invoice URLs (please correct me if I am wrong), I am storing each invoice's Stripe ID in the database and then use that ID to lookup the current URL to the invoice PDF from the Stripe API.</p>
  <p>The problem is that this works <em>most of the time</em> but not all the time. The spec that I created for the controller <code>show</code> action fails in about 20 % of cases because the two URLs do not match:</p>
  <pre><code>describe ReceiptsController, :type =&gt; :controller do
  
    before :each do
      @account = FactoryBot.create(:activated_account)
      @user = @account.users.create(FactoryBot.attributes_for(:user))
      sign_in(@user)
    end
  
    describe 'GET #show' do
  
      # The implementation details of this block don't really matter
      before :each do
        Customers::FindOrCreate.call(@account)
        stripe_subscription = Subscriptions::CreateRemote.call(@account,:payment_behavior =&gt; &quot;default_incomplete&quot;)
        @stripe_invoice = stripe_subscription.latest_invoice
        @receipt = Receipts::Create.call(@stripe_invoice)
      end
  
      # This test fails in about 20 % of cases because the redirect does not go to @stripe_invoice.invoice_pdf but a slightly different URL
      it &quot;redirects to Stripe invoice PDF&quot; do
        get :show, :params =&gt; {:id =&gt; @receipt}
        expect(response).to redirect_to @stripe_invoice.invoice_pdf
      end
  
    end
  
  end
  </code></pre>
  <p>How can this be? Does the <code>invoice_pdf</code> property of a Stripe invoice change every few seconds? I've been trying to work this out for days now but can't get my head around it.</p>
  <hr />
  <p><strong>Addition:</strong></p>
  <p>This is a typical test failure that I get quite often:</p>
  <pre><code>Expected response to be a redirect to &lt;https://pay.stripe.com/invoice/acct_105jfm2HzYSlmhv7/test_YWNjdF8xMDJqc20yS3pZUmxzaHc0LF9NMGZONnFzNUpPTjlObVprd0hvdGpIdWFUamJHTTVxLDQ3Njc3MDY30200oOxX3A1/pdf?s=ap&gt; but was a redirect to &lt;https://pay.stripe.com/invoice/acct_105jfm2HzYSlmhv7/test_YWNjdF8xMDJqc20yS3pZUmxzaHc0LF9NMGZONnFzNUpPTjlObVprd0hvdGpIdWFUamJHTTVxLDQ3Njc3MDY402001iYCSUbn/pdf?s=ap&gt;.
         Expected &quot;https://pay.stripe.com/invoice/acct_105jfm2HzYSlmhv7/test_YWNjdF8xMDJqc20yS3pZUmxzaHc0LF9NMGZONnFzNUpPTjlObVprd0hvdGpIdWFUamJHTTVxLDQ3Njc3MDY30200oOxX3A1F/pdf?s=ap&quot; to be === &quot;https://pay.stripe.com/invoice/acct_105jfm2HzYSlmhv7/test_YWNjdF8xMDJqc20yS3pZUmxzaHc0LF9NMGZONnFzNUpPTjlObVprd0hvdGpIdWFUamJHTTVxLDQ3Njc3MDY402001iYCSUbn/pdf?s=ap&quot;.
  </code></pre>