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