我对PayPal API非常陌生。
我的rails服务器检查每笔付款是否获得批准。我在为它写说明书,因为我不知道我怎么能“伪造”一个批准。
我想出了三种可能性:
paymentId。对我来说这三个人看起来都不太好。
因此,问题仍然存在:,我如何为我的规范创建一个批准的付款?
发布于 2018-07-03 15:25:06
您应该模拟api的响应。您没有必要实际执行请求,但也没有理由不测试处理api请求结果的代码。假设您想要模拟以下代码:
#controller.rb
def initialize(dependencies = {})
@payment_service = dependencies.fetch(:paypal_api) do
Payment
end
end
...
def payment_method
payment = @payment_service.find("PAY-57363176S1057143SKE2HO3A")
if payment.execute(payer_id: "DUFRQ8GWYMJXC")
# Do some stuff
return 'success!'
end
'failure!'
end您可以在Rspec中使用类似的方法来模拟您的响应:
# controller_spec.rb
let(:paypal_api) { double('Payment') }
let(:mock_payment) { double('PayPal::SDK::REST::DataTypes::Payment') }
let(:mock_controller) { described_class.new(paypal_api: paypal_api) }
...
it 'returns the correct result when the payment is successfull' do
mock_response = {
"paymentExecuteResponse":{
"id":,
"intent":"sale",
"state":"approved",
"cart":,
"payer":{
"payment_method":"paypal",
"payer_info":{
"email":,
"first_name":,
"last_name":,
"payer_id":,
"phone":,
"country_code":
}
}
... some other stuff...
}
}
...
# This mocks the find method from the sdk
allow(paypal_api).to receive(:find).and_return(mock_payment)
# This mocks the execution of the payment
allow(mock_payment).to receive(:execute).and_return(mock_response)
result = mock_controller.payment_method
expect(result).to 'success!'
end我还建议大家看看关于双打的rspec文档。
https://stackoverflow.com/questions/51157586
复制相似问题