0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ルーティングを調べる

Last updated at Posted at 2025-02-06

ルーティング仕様

Routing specs

ルーティングの使用はtype: :routingで印にするか、spec/routingディレクトリに置くことによってconfig.infer_spec_type_from_file_location!を設定している場合に印にする

Routing specs are marked by type: :routing or if you have set config.infer_spec_type_from_file_location! by placing them in spec/routing.

標準のRESTfulのルートのみを持つシンプルなアプリはルーティングの仕様から多くの価値を得られないだろう。しかし、カスタマイズされたoutes, like vanity links, slugsなどを明示するため使われたとき重要な価値を供給することができる

Simple apps with nothing but standard RESTful routes won’t get much value from routing specs, but they can provide significant value when used to specify customized routes, like vanity links, slugs, etc.

expect(:get => "/articles/2012/11/when-to-use-routing-specs").to route_to(
  :controller => "articles", <- コントローラ、パラメータを指定したルーティングを指定できる
  :month => "2012-11",
  :slug => "when-to-use-routing-specs"
)

使用可能にすべきではないルートに対しても貴重な存在だ。

They are also valuable for routes that should not be available:

expect(:delete => "/accounts/37").not_to be_routable<-not_toをつけることですべきではないことを表現していると考える

route_toマッチャー

route_to matcher

route_toマッチャーは(verb + path)のリクエストがルーティングの宛先であることを示す。標準のRESTfulルート以外のルートを明示するときとても貴重です。

The route_to matcher specifies that a request (verb + path) is routable. It is most valuable when specifying routes other than standard RESTful routes.

expect(get("/")).to route_to("welcome#index") # new in 2.6.0 <-指定のアクションに行くかどうかを検証

or

expect(:get => "/").to route_to(:controller => "welcome") <-指定のコントローラに行くことも指定できる

verbはhttp動詞を指すので、例えばget("/")はgetリクエストでパスは/を指している

他にも

route_to(:controller => "widgets", :action => "index") 

route_to("widgets#foo")

route_to("admin/accounts#index")

route_to(:controller => "admin/accounts", :action => "index")

be_routableマッチャー

be_routable matcher

be_routableマッチャーはshould_notと一緒に使うことでルーティング可能でない指定のルートを明示するために最適です。ルーティングスペック(spec/controllers)とコントローラスペック(spec/routing)で利用可能です。

The be_routable matcher is best used with should_not to specify that a given route should not be routable. It is available in routing specs (in spec/routing) and controller specs (in spec/controllers).

名前付きルートを使用

Using named routes

ルーティング仕様は名前付きルートへアクセスできる

Routing specs have access to named routes.

to route_to(:controller => "widgets", :action => "new")<-actionが名前付きルートを指していると考える

エンジンルートの使用

Using engine routes

ルーティングはexampleグループに対して使用されるだろうリクエストセットを明示することができる。これはライlsエンジンをテストするときにとても役に立つ。

Routing specs can specify the routeset that will be used for the example group. This is most useful when testing Rails engines.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?