ルーティング仕様
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.