コントローラー内部をテストする
Testing controller internals
session,flash,cookieを書くだけで使える
特定の主張に加えて、常備されたテスト・ユニットに使えるいくつかのコレクションに対して簡単なアクセスできます。それらのコレクションは以下の通りです。
In addition to these specific assertions, you also have easy access to various
collections that the regular test/unit assertions can be used against. These
collections are:
session
:セッション内で保存されるオブジェクト
- session: Objects being saved in the session.
flash
:セッションに現在保存されているフラッシュオブジェクト
- flash: The flash objects currently in the session.
cookie
:リクエスト上でユーザーへ送られるCookie
- cookies: Cookies being sent to the user on this request.
これらコレクションは他のどのハッシュ同様に使用できる
These collections can be used just like any other hash:
> assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave"
assert flash.empty? # makes sure that there's nothing in the flash
コレクションの上で、特定のアクションがリダイレクトした完全なURLはredirect_to_url
で利用可能です。
On top of the collections, you have the complete URL that a given action
redirected to available inredirect_to_url
.
ハッシュのように使用できる
同じコントローラ内でのリダイレクトに対してfollow_redirect
を呼び、リダイレクトを追い、他のアクションコールしてアサートすることもできる。
For redirects within the same controller, you can even call follow_redirect
and the redirect will be followed, triggering another action call which can
then be asserted against.
sessionとcookieの変数を操る
sessionとcookieの変数を操る
Manipulating session and cookie variables
時々テストのためsessionとcookie変数を設定する必要がある。これを行うにはsession、cookieコレクションに対して値を割り当てるだけです
Sometimes you need to set up the session and cookie variables for a test. To
do this just assign a value to the session or cookie collection:
session[:key] = "value"
cookies[:key] = "value"
テストに対してcookieをクリアするには、cookieコレクションをクリアするだけです。
To clear the cookies for a test just clear the cookie collection:
cookies.clear