- B
- C
- D
- G
- H
- P
- Q
- S
- ActionDispatch::TestProcess
- ActiveSupport::Testing::ConstantLookup
- Rails::Dom::Testing::Assertions
- ActionDispatch::Assertions
Attributes
| [R] | request | |
| [R] | response |
实例公共方法
build_response(klass) 链接
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 592 def build_response(klass) klass.create end
controller_class_name() 链接
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 552 def controller_class_name @controller.class.anonymous? ? "anonymous" : @controller.class.controller_path end
delete(action, **args) 链接
模拟具有给定参数的 DELETE 请求,并设置/处理响应。更多详情请参阅 get。
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 463 def delete(action, **args) process(action, method: "DELETE", **args) end
generated_path(generated_extras) 链接
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 556 def generated_path(generated_extras) generated_extras[0] end
get(action, **args) 链接
使用给定的参数模拟 GET 请求。
-
action: 要调用的控制器操作。 -
params: 要传递的 HTTP 参数哈希。此参数可以是nil。 -
body: 请求体,包含适当编码的字符串(application/x-www-form-urlencoded或multipart/form-data)。 -
session: 要存储在会话中的参数哈希。此参数可以是nil。 -
flash: 要存储在 flash 中的参数哈希。此参数可以是nil。
您还可以使用 post、patch、put、delete 和 head 来模拟 POST、PATCH、PUT、DELETE 和 HEAD 请求。例如,发送参数、会话和设置 flash 消息
get :show, params: { id: 7 }, session: { user_id: 1 }, flash: { notice: 'This is flash message' }
请注意,请求方法未经验证。提供不同的方法是为了使测试更具表现力。
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 439 def get(action, **args) process(action, method: "GET", **args) end
head(action, **args) 链接
模拟具有给定参数的 HEAD 请求,并设置/处理响应。更多详情请参阅 get。
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 469 def head(action, **args) process(action, method: "HEAD", **args) end
patch(action, **args) 链接
模拟具有给定参数的 PATCH 请求,并设置/处理响应。更多详情请参阅 get。
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 451 def patch(action, **args) process(action, method: "PATCH", **args) end
post(action, **args) 链接
模拟具有给定参数的 POST 请求,并设置/处理响应。更多详情请参阅 get。
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 445 def post(action, **args) process(action, method: "POST", **args) end
process(action, method: "GET", params: nil, session: nil, body: nil, flash: {}, format: nil, xhr: false, as: nil) 链接
通过指定请求方法、参数并设置/处理响应来模拟对 action 的 HTTP 请求。
-
action: 要调用的控制器操作。 -
method: 用于发送 HTTP 请求的请求方法。可能的值包括GET、POST、PATCH、PUT、DELETE、HEAD。默认为GET。可以是符号。 -
params: 要传递的 HTTP 参数哈希。此参数可以是nil。 -
body: 请求体,包含适当编码的字符串(application/x-www-form-urlencoded或multipart/form-data)。 -
session: 要存储在会话中的参数哈希。此参数可以是nil。 -
flash: 要存储在 flash 中的参数哈希。此参数可以是nil。 -
format: 请求格式。默认为nil。可以是字符串或符号。 -
as: 内容类型。默认为nil。必须是对应于 MIME 类型的符号。
调用 create 操作并发送两个参数的示例
process :create, method: 'POST', params: { user: { name: 'Gaurish Sharma', email: 'user@example.com' } }, session: { user_id: 1 }, flash: { notice: 'This is flash message' }
为了模拟 GET、POST、PATCH、PUT、DELETE 和 HEAD 请求,请分别优先使用 get、post、patch、put、delete 和 head 方法,这将使测试更具表现力。
不建议在同一个测试中执行多个请求。在一个请求中设置的实例变量不会持久化到下一个请求,但不能保证所有 Rails 内部状态都会被重置。请优先使用 ActionDispatch::IntegrationTest 来执行多个请求。
请注意,请求方法未经验证。
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 512 def process(action, method: "GET", params: nil, session: nil, body: nil, flash: {}, format: nil, xhr: false, as: nil) check_required_ivars @controller.clear_instance_variables_between_requests action = +action.to_s http_method = method.to_s.upcase @html_document = nil cookies.update(@request.cookies) cookies.update_cookies_from_jar @request.set_header "HTTP_COOKIE", cookies.to_header @request.delete_header "action_dispatch.cookies" @request = TestRequest.new scrub_env!(@request.env), @request.session, @controller.class @response = build_response @response_klass @response.request = @request @controller.recycle! if body @request.set_header "RAW_POST_DATA", body end @request.set_header "REQUEST_METHOD", http_method if as @request.content_type = Mime[as].to_s format ||= as end parameters = (params || {}).symbolize_keys if format parameters[:format] = format end setup_request(controller_class_name, action, parameters, session, flash, xhr) process_controller_response(action, cookies, xhr) end
put(action, **args) 链接
模拟具有给定参数的 PUT 请求,并设置/处理响应。更多详情请参阅 get。
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 457 def put(action, **args) process(action, method: "PUT", **args) end
query_parameter_names(generated_extras) 链接
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 560 def query_parameter_names(generated_extras) generated_extras[1] + [:controller, :action] end
setup_controller_request_and_response() 链接
Source: 显示 | 在 GitHub 上
# File actionpack/lib/action_controller/test_case.rb, line 564 def setup_controller_request_and_response @controller = nil unless defined? @controller @response_klass = ActionDispatch::TestResponse if klass = self.class.controller_class if klass < ActionController::Live @response_klass = LiveTestResponse end unless @controller begin @controller = klass.new rescue warn "could not construct controller #{klass}" if $VERBOSE end end end @request = TestRequest.create(@controller.class) @response = build_response @response_klass @response.request = @request if @controller @controller.request = @request @controller.params = {} end end