0

I have the following endpoint in payment controller that completes the purchase. I am using rspecs for testing. For mocking stripe i am using webmock gem. Now i would like to test the failure case i.e rescue Stripe::CardError => e part. I have commented below #Failure Case.

   def create

    @cart.user = current_user

    customer = Stripe::Customer.create email: stripe_params['sender_email'],
                                       source: stripe_params["card_token"]

    Stripe::Charge.create customer: customer.id,
                          amount: @cart.subtotal,
                          description: 'Gift Purchase',
                          currency: 'usd'

    render "confirmation"

   rescue Stripe::CardError => e
     #Failure Case
     flash[:alert] = e.message
     redirect_to new_payment_path
   end

My rspecs test case looks like follows. First i am stubbing web request used by stripe. How should i stub the request so that stripe throws Stripe::CardError exception and failure branch is executed. Hope i have made the problem clear. I appreciate any help. Thanks!

 context "invalid purchases" do 

   it "card error" do

       stub_request(:post, "https://api.stripe.com/v1/customers").
         with(
           body: {"email"=>"asd@asd.com"},
           headers: {
          'Accept'=>'*/*',
          'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
          'Authorization'=>'Bearer sk_test_UEBAIinq8viQ875czrsC18ZX',
          'Content-Type'=>'application/x-www-form-urlencoded',
          'User-Agent'=>'Stripe/v1 RubyBindings/3.17.0',
          'X-Stripe-Client-User-Agent'=>'{"bindings_version":"3.17.0","lang":"ruby","lang_version":"2.3.4 p301 (2017-03-30)","platform":"x86_64-linux","engine":"ruby","publisher":"stripe","uname":"Linux version 4.9.120-c9 (root@b1cff69ce3c3) (gcc version 8.2.0 (Debian 8.2.0-4) ) #1 SMP Wed Aug 15 22:48:26 UTC 2018","hostname":"kofhearts-ktmbasket-6224565"}'
           }).
         to_return(status: 200, body: {id: 1}.to_json, headers: {})




     stub_request(:post, "https://api.stripe.com/v1/charges").
         with(
           body: {"amount"=>"123", "currency"=>"usd", "customer"=>"1", "description"=>"Gift Purchase"},
           headers: {
          'Accept'=>'*/*',
          'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
          'Authorization'=>'Bearer sk_test_UEBAIinq8viQ875czrsC18ZX',
          'Content-Type'=>'application/x-www-form-urlencoded',
          'User-Agent'=>'Stripe/v1 RubyBindings/3.17.0',
          'X-Stripe-Client-User-Agent'=>'{"bindings_version":"3.17.0","lang":"ruby","lang_version":"2.3.4 p301 (2017-03-30)","platform":"x86_64-linux","engine":"ruby","publisher":"stripe","uname":"Linux version 4.9.120-c9 (root@b1cff69ce3c3) (gcc version 8.2.0 (Debian 8.2.0-4) ) #1 SMP Wed Aug 15 22:48:26 UTC 2018","hostname":"kofhearts-ktmbasket-6224565"}'
           }).
         to_return(status: 200, body: {id: 1}.to_json, headers: {})



      visit root_path

      expect(page).to have_content('teddy')
      click_button('Add to Basket')
      expect(page).to have_content('Your Basket')
      click_link('Checkout')
      click_link('Guest Checkout')

      find('label', :text => 'Credit Card').click
      fill_in 'card_number', with: '4242424242424242'
      fill_in 'card_verification', with: '123'
      fill_in 'exp_month', with: '12'
      fill_in 'exp_year', with: '19'

      click_button('Submit')



   end

 end
kofhearts
  • 3,607
  • 8
  • 46
  • 79
  • I have not used the gem you're using, I had used [this](https://github.com/rebelidealist/stripe-ruby-mock) – Kedarnag Mukanahallipatna Sep 06 '18 at 02:37
  • Possible duplicate of [How to raise an exception in an RSpec test](https://stackoverflow.com/questions/32354356/how-to-raise-an-exception-in-an-rspec-test) – Jagdeep Singh Sep 06 '18 at 03:57
  • @JagdeepSingh this is not a duplicate. i donot want to raise an exception but find out what triggers the exception when testing. – kofhearts Sep 06 '18 at 06:23
  • When writing tests, you already know whether they will raise error or not based on the data you sent to gateway. _"find out what triggers the exception when testing"_ - What does that phrase mean? – Jagdeep Singh Sep 06 '18 at 06:32

1 Answers1

1

For mocking Card errors, and you can find more examples here

it "mocks a declined card error" do
  # Prepares an error for the next create charge request
  StripeMock.prepare_card_error(:card_declined)

  expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to raise_error {|e|
    expect(e).to be_a Stripe::CardError
    expect(e.http_status).to eq(402)
    expect(e.code).to eq('card_declined')
  }
end
  • thanks but i would prefer not to add any additional dependencies. i would appreciate if you can suggest a method to achieve the card error with just webmock. i have tried returning status 500 but no success. – kofhearts Sep 06 '18 at 02:43
  • When you are creating a charge, have you tried not sending any `customer` or `source` and expecting response ? From my understanding, if you don't pass proper information, you should be using `4xx` errors and not `5xx` – Kedarnag Mukanahallipatna Sep 06 '18 at 02:58
  • i have tried that and no effect. i removed customer from request. i dont think this will work because it is just a stub request not actual request so i dont think changing request params will have any effect. – kofhearts Sep 06 '18 at 03:04
  • my guess is to_return(status: 200, body: {id: 1}.to_json, headers: {}) needs to be altered but i dont know what it needs to be in order to trigger Stripe::CardError. still trying. – kofhearts Sep 06 '18 at 03:06
  • I'm looking into it as well. Since you're stubbing changing the request `params`, will not change anything. We need to figure out, how to handle the response. – Kedarnag Mukanahallipatna Sep 06 '18 at 03:18
  • See if [this](https://github.com/stripe/stripe-ruby/blob/27f39aed85b596fb276c898b56f77c87e619deae/test/stripe/charge_test.rb#L43) can help – Kedarnag Mukanahallipatna Sep 06 '18 at 03:22