如何制作 Fake HTTP 响应对象以在 Perl 中进行测试
我编写了一个 Perl 脚本,将数据输入到 Web 服务中。
我对 perl 脚本进行了一些系统测试,检查我是否可以与 web 服务交互,这些测试工作得很好,但我不想在进行小更改时运行系统测试 - 我想运行单元测试:
到目前为止我编写了导入器的一个子类,它在实际调用有问题的 URL 之前简单地拦截 Web 请求,并测试所有输入的类型和形式是否正确,并且在所有情况下都可以正常工作,除非 perl 脚本需要阅读响应以获取说明,然后继续下一步。
我的问题是我无法伪造响应对象。
我尝试过使用 HTTP::Response->new,但它一直抱怨错误的标头参数
如何最好地伪造响应对象?
I have written a perl script that feeds data into a web service.
I have some system tests for the perl script that check that I can interact with the webservice, and these work just fine, but I do not want to be running system tests when I make small changes - I want to run unit tests:
So far I have written a subclass of my importer that simply intercepts the web requests before it actually calls the URL in question and tests that all the inputs are of the right type and form, and this works fine in all cases except where the perl script needs to read the response for instructions, and then proceed to the next steps.
My problem is that I cannot fake a response object.
I've tried using HTTP::Response->new, but it keeps complaining about bad header arguments
How do I best FAKE a response object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需模拟 HTTP::Response 对象。它们很容易构建——至少和模拟一样容易,并且不太可能在测试中引入错误。您需要阅读文档而不仅仅是猜测用法。
当然,您可以在代码中构造它们,但我过去不止一次所做的只是保存
curl
的输出或针对应用程序发出的字符串化请求并将其解析回来成一个物体。尝试玩弄这些——
There is no need to mock the HTTP::Response object. They are easy to construct—at least as easy as mocking would be and less likely to introduce bugs into the tests. You need to read the documentation and not just guess at usage.
You can construct them in code, of course, but what I've done in the past more than once is just save the output of
curl
or a stringified request that was made against an application and parse it back into an object.Try playing around with these–
您可能正在寻找模拟对象 - 在本例中是模拟 LWP 对象?
请参阅 CPAN 上的 Test::Mock::LWP。
其文档显示的用法如下:
如果您在 CPAN 中搜索 Test:: Mock,有相当多的模块用于模拟/伪造对象以进行测试。
You may be looking for mock objects - in this case a mock LWP object?
See Test::Mock::LWP on CPAN.
Its documentation shows usage like this:
If you search CPAN for Test::Mock, there are quite a few modules for mocking/faking objects for testing.