【问题标题】:ActionController::TestCase mock PayPal purchaseActionController::TestCase 模拟 PayPal 购买
【发布时间】:2013-02-13 08:14:03
【问题描述】:

有没有办法模拟购买PaypalExpressGateway

我使用了两个操作来处​​理 PayPal 付款, 第一个操作将用户带到贝宝沙箱登录,并将重定向回我的应用程序以确认付款

但是PAYPAL_GATEWAY.purchase方法不成功,返回错误信息Payment has not been authorized by the user.

我猜这是由于跳过了授权用户购买的步骤

有没有办法让我伪造或模拟 PayPal 来接受交易?

我的ActionController::TestCase 测试操作products#initiate_payment products#confirm_payment 将这些操作称为

post :initiate_payment, initiate_payment_action_params(@product)
get :confirm_payment, confirm_action_params(@product, product_purchased, paypal_token)

并设置贝宝回调响应,然后重定向到贝宝登录

@setup_response = PAYPAL_GATEWAY.setup_purchase(amount, ip: request.remote_ip,
      items: [{name: @product.name, quantity: 1, description: @product.description, amount: amount}],
      currency: 'USD',
      return_url: url_for(action: 'confirm_payment',
                                 product_purchased: @product_purchased.id,                                   
                                 only_path: false),    
      cancel_return_url: url_for(action: 'index',
                                 product_purchased: @product_purchased.id,
                                 only_path: false))

redirect_to PAYPAL_GATEWAY.redirect_url_for(@setup_response.token)

我在products#confirm_payment 操作中处理了购买交易 我将 confirm_payment 的参数嘲笑为

{"product_purchased"=>"10", "token"=>"EC-91J25480XA799581U", "PayerID"=>"4QBC9Y658K6MA", "id"=>"55", "controller"=>"products", "action"=>"confirm_payment"}

get :confirm_payment, confirm_action_params(@product, product_purchased, paypal_token)

PAYPAL_GATEWAY 的配置

PAYPAL_GATEWAY ||= ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)

我还没有找到任何方法来实现这一点,所以这可能会对他们中的许多人有所帮助...

除此之外的任何更好的解决方案将不胜感激

提前致谢

【问题讨论】:

    标签: ruby-on-rails-3 unit-testing paypal functional-testing activemerchant


    【解决方案1】:

    简短的回答是,您必须步行并进行测试购买。另外,这将使您能够体验买家从头到尾的体验。您尝试跳过一两步是否有特殊原因?

    一些商家也会只通过流程一次,并捕获发回的信息,然后使用相同的字符串创建一个表单页面,他们只是使用该页面将信息发布回他们的页面进行测试。这样他们就不必一遍又一遍地通过流程,但这可能会或可能不会工作,具体取决于您的设置。

    大多数情况下选择选项 A 并从头到尾完成流程。

    【讨论】:

    • @PP_MT_Chad 感谢您的帮助。但这是一个功能测试用例,无法真正访问贝宝网站并完成输入凭据的步骤并继续执行其余过程。是的,集成测试在这里会很方便,但尝试在没有 Selenium 或 Web 驱动程序的情况下测试它。
    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2014-04-16
    • 2015-02-28
    • 2013-03-14
    • 2018-02-27
    相关资源
    最近更新 更多