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?

More than 5 years have passed since last update.

リダイレクト先に指定のFlash messagesが表示されているか確認したい

Posted at

やりたいこと

ログインに失敗したときにFlash messagesがちゃんと表示されているかのテストをしたい

下記のコードじゃうまくいかないんじゃ

    public function testLoginUnregisteredUser()
    {
        $data = [
            'email' => 'thisisunregistereduser@hoge.com',
            'password' => 'thisisunregistereduser',
        ];

        $response = $this->post('login', $data);

        $response->assertStatus(302);
        $response->assertSeeText('アカウントが登録されていません');
    }

リダイレクトのレスポンスが302で返されるのでassertSeeTextでは確認できません。

そんなときはassertSessionHasを使うのじゃ

こんなときは直接Sessionを見に行くと吉

    public function testLoginUnregisteredUser()
    {
        $data = [
            'email' => 'thisisunregistereduser@hoge.com',
            'password' => 'thisisunregistereduser',
        ];

        $response = $this->post('login', $data);

        $response->assertStatus(302);
        $response->assertSessionHas('warning', 'アカウントが登録されていません');
    }

参考にしたページ

How do I test a view file after redirect?

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?