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?

テスト時のcookieとsessionを取得する

Posted at

コントローラー内部をテストする

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 in redirect_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

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?