需要朝着正确的方向推动,在 Rails 中编写我的第一个功能测试

发布于 2024-09-01 22:10:25 字数 417 浏览 6 评论 0原文

在过去的几天里,我安静地阅读了一些有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑红尘 2024-09-08 22:10:25

您是否阅读过测试 Rails 应用程序指南?非常好。

你的测试可能看起来像这样:

def test_should_get_index
  get :index, :a => "1", :b => "2", :c => "3"
  assert_response :success
  assert_equal "true", @response.body
end

Have you read A Guide to Testing Rails Applications? It's pretty good.

Your test is probably going to look something like:

def test_should_get_index
  get :index, :a => "1", :b => "2", :c => "3"
  assert_response :success
  assert_equal "true", @response.body
end
春夜浅 2024-09-08 22:10:25

如果这是对您自己的应用程序的测试,则可以根据您的喜好将类似的事情作为功能测试或集成测试来完成。集成测试更加“真实”,因为它们使用真实的 URL 和路由,这与仅执行特定控制器操作的功能测试不同。

对于外部服务,您真正需要的只是使用 Test::Unit 或其他框架(如 rspec 或 Cucumber)作为测试定义的包装器。

在某种类中定义发出此 GET 请求的方法,然后编写一个测试它的工具。例如:

def test_expected_response
  assert_equal 'true', MyHelper.make_call('param_a', 'param_b', 'param_c')
end

显然,除非正确定义了 MyHelper 和 MyHelper.make_call,否则此测试将失败,但这并不太难:

class MyHelper
  def make_call(a, b, c)
    # Call service using Net::HTTP, Curb, etc.
    # ...
    # Return answer
  end
end

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:

def test_expected_response
  assert_equal 'true', MyHelper.make_call('param_a', 'param_b', 'param_c')
end

Obviously this test will fail unless MyHelper and MyHelper.make_call are properly defined, but that isn't too hard:

class MyHelper
  def make_call(a, b, c)
    # Call service using Net::HTTP, Curb, etc.
    # ...
    # Return answer
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文