Integration
測試方法,例如 Integration::RequestHelpers#get
和 Integration::RequestHelpers#post
,會傳回 TestResponse
類別的物件,用來表示所要求控制器動作的 HTTP 回應結果。
請參閱 Response
以取得更多關於控制器回應物件的資訊。
方法
類別公開方法
from_response(response) 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/test_response.rb, line 13 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
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/test_response.rb, line 49 def parsed_body @parsed_body ||= response_parser.call(body) end
response_parser() 連結
來源:顯示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/testing/test_response.rb, line 53 def response_parser @response_parser ||= RequestEncoder.parser(media_type) end