需要朝着正确的方向推动,在 Rails 中编写我的第一个功能测试
在过去的几天里,我安静地阅读了一些有关 Rails 测试的文档,我正在坐下来编写我的第一个真正的测试,但不确定如何将我所学到的知识结合在一起以实现以下功能测试(测试控制器)
我需要向 URL 发送 GET 请求并传递 3 个参数(简单的 Web 服务),如果该功能有效,则简单返回关键字 true
,否则返回关键字 false
被返回 - 它是唯一返回的值 &不包含在任何
、
或其他标签中。测试应断言,如果返回“true”,则测试成功。
这可能非常简单,所以对这样一个没有挑战性的问题表示歉意。
如果有人能指出我如何开始的写作方向,特别是如何测试响应,我将非常感激!
I've read quiet a bit of documentation over the last few days about testing in Rails, I'm sitting down to write my first real test and not 100% sure how to tie what I have learned together to achieve the following functional test (testing a controller)
I need to send a GET request to a URL and pass 3 parameters (simple web-service), if the functionality works the keyword true
is simply returned, otherwise the keyword false
is returned - its in only value returned & not contained in any <div>
, <span>
or other tags.
The test should assert that if "true" is returned the test is successful.
This is probably very simple so apologies for such a non-challenging question.
If anyone could point me in the write direction on how I can get started, particularly how I can test the response, I'd be very grateful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否阅读过测试 Rails 应用程序指南?非常好。
你的测试可能看起来像这样:
Have you read A Guide to Testing Rails Applications? It's pretty good.
Your test is probably going to look something like:
如果这是对您自己的应用程序的测试,则可以根据您的喜好将类似的事情作为功能测试或集成测试来完成。集成测试更加“真实”,因为它们使用真实的 URL 和路由,这与仅执行特定控制器操作的功能测试不同。
对于外部服务,您真正需要的只是使用 Test::Unit 或其他框架(如 rspec 或 Cucumber)作为测试定义的包装器。
在某种类中定义发出此 GET 请求的方法,然后编写一个测试它的工具。例如:
显然,除非正确定义了 MyHelper 和 MyHelper.make_call,否则此测试将失败,但这并不太难:
If it's a test of your own application, something like this can be done as a functional test or an integration test depending on your preference. Integration tests are more "real world" in that they use real URLs and routes, unlike functional tests which simply exercise particular controller actions.
For an external service, all you really need is to make use of Test::Unit or another framework like rspec or Cucumber as a wrapper for your test definitions.
Define the method that makes this GET request in a class of some kind, and then write a harness that tests it. For example:
Obviously this test will fail unless MyHelper and MyHelper.make_call are properly defined, but that isn't too hard: