Ad

Laravel5 Unit Testing A Login Form

- 1 answer

I ran the following test and I am receiving a failed_asserting that false is true. Can someone further explain why this could be?

/** @test */
public function a_user_logs_in()
{
    $user =  factory(App\User::class)->create(['email' => '[email protected]', 'password' => bcrypt('testpass123')]);

    $this->visit(route('login'));
    $this->type($user->email, 'email');
    $this->type($user->password, 'password');
    $this->press('Login');
    $this->assertTrue(Auth::check());
    $this->seePageIs(route('dashboard'));
}
Ad

Answer

Your PHPUnit test is a client, not the web application itself. Therefore Auth::check() shouldn't return true. Instead, you could check that you are on the right page after pressing the button and that you see some kind of confirmation text:

    /** @test */
    public function a_user_can_log_in()
    {
        $user = factory(App\User::class)->create([
             'email' => '[email protected]', 
             'password' => bcrypt('testpass123')
        ]);

        $this->visit(route('login'))
            ->type($user->email, 'email')
            ->type('testpass123', 'password')
            ->press('Login')
            ->see('Successfully logged in')
            ->onPage('/dashboard');
    }

I believe this is how most developers would do it. Even if Auth::check() worked – it would only mean a session variable is created, you would still have to test that you are properly redirected to the right page, etc.

Ad
source: stackoverflow.com
Ad