简体   繁体   English

如何在Rails测试用例中发送不明确的请求?

[英]How can I send an ambiguous request in Rails Test Case?

I have the following definitions in my routes: 我的路线中有以下定义:

resources :demands { resources: :solutions }
resources :technologies { resources: :solutions }

and in my controller test, I want to send two requests: 在控制器测试中,我想发送两个请求:

Get /demands/d123/solutions?technology_id=t123
Get /technologies/t123/solutions?demand_id=d123

But they both match the same invokes(in SolutionsControllerTest): 但是它们都匹配相同的调用(在SolutionsControllerTest中):

get :index, demand_id: d123, technology_id: t123

it match the first definition in routes.rb, which is 它匹配routes.rb中的第一个定义,即

resources :demands { resources: :solutions }

How should I send both requests in distinct methods? 我应该如何以不同的方法发送两个请求?

Functional (Controller) tests fake the whole request stage. 功能(控制器)测试在整个请求阶段都是伪造的。 Instead of invoking a rails server and sending an actual request ActionController::TestCase just creates a Request object and passes it to a controller instance. 而不是调用Rails服务器并发送实际的请求, ActionController::TestCase只是创建一个Request对象,并将其传递给控制器​​实例。 This lets you poke around in way which is not possible in an integration test due to the server and test running in separate processes. 这样一来,您就可以在集成测试中无法实现,因为服务器和测试在单独的进程中运行。

Instead you would want to use a integration test and actually send a HTTP request so that it goes through the routing component: 相反,您希望使用集成测试并实际发送HTTP请求,以便它通过路由组件:

require 'test_helper'

class AmbiguousRoutesTest < ActionDispatch::IntegrationTest
  test "is /demands/d123/solutions?technology_id=t123 ambigous?" do
     get "/demands/d123/solutions?technology_id=t123"
     # @todo write assertions about the response
  end 
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM