3

有沒有辦法來嘲笑PaypalExpressGateway的ActionController :: TestCase的模擬貝寶購買

的購買

我已經使用貝寶支付處理兩個動作, 第一個動作將用戶帶到貝寶沙箱登錄並回重定向到我申請付款確認

PAYPAL_GATEWAY.purchase方法沒有成功,並返回一個錯誤信息Payment has not been authorized by the user.

嗯,我想這是因爲由用戶

012授權購買跳過步

有沒有辦法讓我假裝或模擬貝寶接受交易?

ActionController::TestCase測試行動products#initiate_paymentproducts#confirm_payment調用這些行動是

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

而設置PayPal的回調響應然後重定向到登錄PayPal

@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)

我還沒有發現任何的方法來實現這一所以這可能會幫助許多人...

以外,這將是最沒有更好的解決方案讚賞

在此先感謝

回答

1

簡短的回答是,你會^ h大步走過,並進行測試購買。另外,這可以讓你通過買家從頭到尾的體驗。你有沒有特別的理由想要跳過一兩步?

一些商家也會一次遍歷流程,捕獲發回的信息,然後使用相同的字符串創建表單頁面,他們只是用來將信息發佈回其頁面進行測試。這樣他們就不必一遍又一遍地流過這個流程,但這可能會或可能不會運行,具體取決於您的設置。

最典型的是使用選項A並從頭到尾瀏覽流程。

+0

@PP_MT_Chad感謝您的幫助。 但是,這是一個功能測試案例,實際上無法訪問貝寶網站並完成輸入憑證並繼續執行其餘過程的步驟。是的,集成測試在這裏會很方便,但是如果沒有Selenium或Web驅動程序,嘗試一下方法來測試它。 – swapab 2013-02-27 15:45:19