我正在使用rspec和capybara编写测试用例,以便与Cheddargetter(付款解决方案提供商)集成。我没有问题测试我的CG API的请求,但我不知道如何最好地测试CG的API给Web应用程序的回调。如何测试webhooks /远程回调?
这与PayPal的IPN功能类似,客户支付Web钩子回调后发送到您的应用程序。
想知道是否有人知道什么是测试/模拟这个最好的方法?
我正在使用rspec和capybara编写测试用例,以便与Cheddargetter(付款解决方案提供商)集成。我没有问题测试我的CG API的请求,但我不知道如何最好地测试CG的API给Web应用程序的回调。如何测试webhooks /远程回调?
这与PayPal的IPN功能类似,客户支付Web钩子回调后发送到您的应用程序。
想知道是否有人知道什么是测试/模拟这个最好的方法?
你可能使用的是控制器来处理POST
请求,让我们把它WebhookController
你可以简单地测试与你所需要的PARAMS后做你想要什么。例如,我整合测试(在测试单位但rspec做同样的事情)。
Rspec对于上传/添加xml文件可能有不同版本的fixture_file_upload
,但according to this stack question看起来您也可以使用它。坚持档案说spec/files
。
无论如何,对于网络和新手来说,你会测试你的Delayed::Job
调用实际上在另一个测试中有效。 喜欢的东西:
class GetWebhookTest < ActionController::IntegrationTest
fixtures :all
def recieve_webhook
post '/webhook/338782', fixture_file_upload('webhook.xml', 'application/xml')
end
#Test you do what the outcome of your POST would be.
#this is refactored but you can shove the post line where receive_webhook is
test "recieve a webhook via xml" do
assert_difference('RawData.count') do
receive_webhook
end
end
test "make sure the status is 200" do
recieve_webhook
assert_response :success
end
#Test 1 will fail before this, but i was more/too thorough back in the day
test "Delayed Job increases" do
assert_difference "Delayed::Job.count", 1 do
recieve_webhook
end
end
end
再次,Rspec的有之类的东西response.should be_success
及其Object.count差分法了。根据你的情况调整。关键是fixture_file_upload
这看起来像是一个很好的方法。任何现有的宝石可以帮助进行这种测试吗?尤其是为了帮助保存现有的POST请求并将其存储到文件以便稍后重用(如VCR,但用于回调而不是请求) – redshift5
我想你想在'postbin'中记录实际的呼叫(我认为服务是所谓的)。就像这个http://requestb.in/,你会得到你需要的xml。之后,它是fixture_file_upload和真正的测试。我见过的大多数宝石都是外发的,而不是传入的,这就是fixture_file_upload的作用。海事组织。 – pjammer
谢谢,虽然requestb.in服务似乎因为某些原因被打破。如果我在Web控制台中检查AJAX调用,会出现500内部错误。我甚至检出并将自己的版本部署到Heroku,并具有相同的问题。我会寻找替代服务。你能想到另一个头脑吗?谢谢你的方式 – redshift5