【问题标题】:how to test stripe failure case with rspecs?如何使用 rspecs 测试条带故障案例?
【发布时间】:2019-02-11 05:21:08
【问题描述】:

我在完成购买的支付控制器中有以下端点。我正在使用 rspecs 进行测试。对于模拟条纹,我正在使用 webmock gem。现在我想测试失败案例,即救援 Stripe::CardError => e 部分。我已在#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

我的 rspecs 测试用例如下所示。首先,我正在存根条带使用的 Web 请求。我应该如何存根请求以便条带抛出 Stripe::CardError 异常并执行失败分支。希望我已经把问题说清楚了。我很感激任何帮助。谢谢!

 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

【问题讨论】:

  • 我没用过你用的gem,我用过this
  • @JagdeepSingh 这不是重复的。我不想引发异常,但要找出测试时触发异常的原因。
  • 在编写测试时,您已经知道它们是否会根据您发送到网关的数据引发错误。 “找出测试时触发异常的原因” - 这句话是什么意思?

标签: ruby-on-rails rspec stripe-payments webmock


【解决方案1】:

用于模拟卡错误,您可以找到更多示例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

【讨论】:

  • 谢谢,但我不希望添加任何额外的依赖项。如果您能建议一种仅使用 webmock 来实现卡错误的方法,我将不胜感激。我尝试返回状态 500 但没有成功。
  • 创建收费时,您是否尝试过不发送任何customersource 并期待回复?据我了解,如果您没有传递正确的信息,您应该使用4xx 错误而不是5xx
  • 我试过了,没有效果。我从请求中删除了客户。我不认为这会起作用,因为它只是一个存根请求而不是实际请求,所以我认为更改请求参数不会有任何影响。
  • 我的猜测是 to_return(status: 200, body: {id: 1}.to_json, headers: {}) 需要更改,但我不知道它需要什么才能触发 Stripe ::卡错误。还在尝试。
  • 我也在调查它。由于您正在更改请求params,因此不会更改任何内容。我们需要弄清楚如何处理响应。
猜你喜欢
  • 2013-08-26
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 2010-12-23
  • 2016-02-05
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多