如何测试webhooks /远程回叫?

时间:2012-11-08 11:30:04

标签: ruby-on-rails rspec capybara

我正在使用rspec和capybara编写测试用例以与Cheddargetter(支付解决方案提供商)集成。我测试我对CG API的请求没有问题但是我不确定如何在CG的API给Rails应用程序提供Web回调的时候进行最佳测试。

这类似于PayPal的IPN功能,在客户支付了网络连接后,回调被发送到您的应用。

只是想知道是否有人知道测试/模拟这个的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

您可能正在使用控制器处理POST个请求,我们称之为WebhookController

您可以简单地测试一个帖子,其中包含您需要的所有内容正在执行您想要的内容。例如,我的综合测试(在测试单元中,但rspec做同样的事情)。

Rspec可能有不同版本的fixture_file_upload用于上传/添加xml文件,但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

相关问题