跳到內容 跳到搜尋

一組斷言,用於測試 Rails 產生的路由以及處理對這些路由發出的請求。

命名空間
方法
A
M
W

實例公開方法

assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil)

斷言可以使用提供的選項產生提供的路徑。這是 assert_recognizes 的相反。extras 參數用於告訴請求其他請求參數(將會出現在查詢字串中)的名稱和值。message 參數允許您指定自訂的錯誤訊息,以顯示於斷言失敗時。

defaults 參數未被使用。

# Asserts that the default action is generated for a route with no action
assert_generates "/items", controller: "items", action: "index"

# Tests that the list action is properly routed
assert_generates "/items/list", controller: "items", action: "list"

# Tests the generation of a route with a parameter
assert_generates "/items/list/1", { controller: "items", action: "list", id: "1" }

# Asserts that the generated route gives us our custom route
assert_generates "changesets/12", { controller: 'scm', action: 'show_diff', revision: "12" }
# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 204
def assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil)
  if expected_path.include?("://")
    fail_on(URI::InvalidURIError, message) do
      uri = URI.parse(expected_path)
      expected_path = uri.path.to_s.empty? ? "/" : uri.path
    end
  else
    expected_path = "/#{expected_path}" unless expected_path.start_with?("/")
  end

  options = options.clone
  generated_path, query_string_keys = @routes.generate_extras(options, defaults)
  found_extras = options.reject { |k, _| ! query_string_keys.include? k }

  msg = message || sprintf("found extras <%s>, not <%s>", found_extras, extras)
  assert_equal(extras, found_extras, msg)

  msg = message || sprintf("The generated path <%s> did not match <%s>", generated_path,
      expected_path)
  assert_equal(expected_path, generated_path, msg)
end

assert_recognizes(expected_options, path, extras = {}, msg = nil)

斷言已正確處理給定 path 的路由,且已剖析的選項(給予 expected_options 散列中)符合 path。基本上,它斷言 Rails 辨識由 expected_options 給出的路由。

傳遞一個散列至第二個參數(path),以指定請求方法。這對於需要特定 HTTP 方法的路由很有用。散列應包含一個 :path,其中包含輸入請求路徑,以及一個 :method,其中包含所需的 HTTP 動詞。

# Asserts that POSTing to /items will call the create action on ItemsController
assert_recognizes({controller: 'items', action: 'create'}, {path: 'items', method: :post})

您也可以傳遞 extras,其中包含一般會出現在查詢字串中的 URL 參數的散列。這可用於斷言查詢字串中的值將正確顯示在 params 散列中。要測試查詢字串,您必須使用 extras 參數,因為直接將查詢字串附加至路徑將不會運作。例如

# Asserts that a path of '/items/list/1?view=print' returns the correct options
assert_recognizes({controller: 'items', action: 'list', id: '1', view: 'print'}, 'items/list/1', { view: "print" })

message 參數允許您傳遞在失敗時顯示的錯誤訊息。

# Check the default route (i.e., the index action)
assert_recognizes({controller: 'items', action: 'index'}, 'items')

# Test a specific action
assert_recognizes({controller: 'items', action: 'list'}, 'items/list')

# Test an action with a parameter
assert_recognizes({controller: 'items', action: 'destroy', id: '1'}, 'items/destroy/1')

# Test a custom route
assert_recognizes({controller: 'items', action: 'show', id: '1'}, 'view/item1')
# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 164
def assert_recognizes(expected_options, path, extras = {}, msg = nil)
  if path.is_a?(Hash) && path[:method].to_s == "all"
    [:get, :post, :put, :delete].each do |method|
      assert_recognizes(expected_options, path.merge(method: method), extras, msg)
    end
  else
    request = recognized_request_for(path, extras, msg)

    expected_options = expected_options.clone

    expected_options.stringify_keys!

    msg = message(msg, "") {
      sprintf("The recognized options <%s> did not match <%s>, difference:",
              request.path_parameters, expected_options)
    }

    assert_equal(expected_options, request.path_parameters, msg)
  end
end

assert_routing(path, options, defaults = {}, extras = {}, message = nil)

斷言 path 和 options 兩個方向上都符合;換句話說,它驗證 path 產生 options,然後 options 產生 path。這基本上將 assert_recognizesassert_generates 結合為一個步驟。

extras 散列允許您指定一般會作為查詢字串提供給動作的選項。message 參數允許您指定自訂錯誤訊息,以顯示於失敗時。

# Asserts a basic route: a controller with the default action (index)
assert_routing '/home', controller: 'home', action: 'index'

# Test a route generated with a specific controller, action, and parameter (id)
assert_routing '/entries/show/23', controller: 'entries', action: 'show', id: 23

# Asserts a basic route (controller + default action), with an error message if it fails
assert_routing '/store', { controller: 'store', action: 'index' }, {}, {}, 'Route for store index not generated properly'

# Tests a route, providing a defaults hash
assert_routing 'controller/action/9', {id: "9", item: "square"}, {controller: "controller", action: "action"}, {}, {item: "square"}

# Tests a route with an HTTP method
assert_routing({ method: 'put', path: '/product/321' }, { controller: "product", action: "update", id: "321" })
# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 248
def assert_routing(path, options, defaults = {}, extras = {}, message = nil)
  assert_recognizes(options, path, extras, message)

  controller, default_controller = options[:controller], defaults[:controller]
  if controller && controller.include?(?/) && default_controller && default_controller.include?(?/)
    options[:controller] = "/#{controller}"
  end

  generate_options = options.dup.delete_if { |k, _| defaults.key?(k) }
  assert_generates(path.is_a?(Hash) ? path[:path] : path, generate_options, defaults, extras, message)
end

method_missing(selector, ...)

路線 TODO:這些聲明應在整合情境中運作

# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 261
def method_missing(selector, ...)
  if @controller && @routes&.named_routes&.route_defined?(selector)
    @controller.public_send(selector, ...)
  else
    super
  end
end

with_routing(config = nil, &block)

一個協助測試不同路線組態的函式。這個方法會暫時將 @routes 替換成新的 RouteSet 執行個體。

新的執行個體會產生於傳遞區塊。通常區塊會使用 set.draw { match ... } 產生一些路徑

with_routing do |set|
  set.draw do
    resources :users
  end
  assert_equal "/users", users_path
end
# File actionpack/lib/action_dispatch/testing/assertions/routing.rb, line 121
def with_routing(config = nil, &block)
  old_routes, old_controller = @routes, @controller
  create_routes(config, &block)
ensure
  reset_routes(old_routes, old_controller)
end