跳至内容 跳至搜索

Integration 测试方法,如 Integration::RequestHelpers#getIntegration::RequestHelpers#post 返回 TestResponse 类的对象,这些对象代表请求的控制器操作的 HTTP 响应结果。

有关控制器响应对象的更多信息,请参见 Response

方法
F
P
R

类公共方法

from_response(response)

# File actionpack/lib/action_dispatch/testing/test_response.rb, line 14
def self.from_response(response)
  new response.status, response.headers, response.body
end

实例公共方法

parsed_body()

根据响应 MIME 类型返回解析后的主体。当找不到与 MIME 类型相对应的解析器时,它将返回原始主体。

示例

get "/posts"
response.content_type         # => "text/html; charset=utf-8"
response.parsed_body.class    # => Nokogiri::HTML5::Document
response.parsed_body.to_html  # => "<!DOCTYPE html>\n<html>\n..."

assert_pattern { response.parsed_body.at("main") => { content: "Hello, world" } }

response.parsed_body.at("main") => {name:, content:}
assert_equal "main", name
assert_equal "Some main content", content

get "/posts.json"
response.content_type         # => "application/json; charset=utf-8"
response.parsed_body.class    # => Array
response.parsed_body          # => [{"id"=>42, "title"=>"Title"},...

assert_pattern { response.parsed_body => [{ id: 42 }] }

get "/posts/42.json"
response.content_type         # => "application/json; charset=utf-8"
response.parsed_body.class    # => ActiveSupport::HashWithIndifferentAccess
response.parsed_body          # => {"id"=>42, "title"=>"Title"}

assert_pattern { response.parsed_body => [{ title: /title/i }] }

response.parsed_body => {id:, title:}
assert_equal 42, id
assert_equal "Title", title
# File actionpack/lib/action_dispatch/testing/test_response.rb, line 50
def parsed_body
  @parsed_body ||= response_parser.call(body)
end

response_parser()

# File actionpack/lib/action_dispatch/testing/test_response.rb, line 54
def response_parser
  @response_parser ||= RequestEncoder.parser(media_type)
end