Laravel5 Unit Testing the login form

I performed the following test and I get fail_asserting that false is true. Can anyone else explain why this might be?

/** @test */
public function a_user_logs_in()
{
    $user =  factory(App\User::class)->create(['email' => 'john@example.com', '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'));
}
+4
source share
2 answers

Your PHPUnit test is the client, not the web application itself. Therefore, Auth :: check () should not return true. Instead, you can verify that you are on the correct page after clicking 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' => 'john@example.com', 
             'password' => bcrypt('testpass123')
        ]);

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

, . Auth:: check() - , , ..

+5

, , → be ($ user), .

, API

    $user = new User(['name' => 'peak']);
    $this->be($user)
         ->get('/api/v1/getManufacturer')
          ->seeJson([
             'status' => true,
         ]);

+2

Source: https://habr.com/ru/post/1623702/


All Articles