pn51291's avatar

Feature test return unexpected value

Hi guys, I'm learning about testing in Laravel with PHPUNIT. Currently, I'm testing "user login with correct credential". I expect it will redirect to a user dashboard, but PHPUNIT keep redirect to hompage.

I also test it in browser, and it redirect to the page I want. Only PHPUNIT redirect to the wrong path.

Thanks in advance :)

0 likes
14 replies
pn51291's avatar
public function test_user_login_with_correct_credentials()
    {
        $user = \App\User::all()[0];

        $response = $this->post(route('user.login.submit'), [
            'email' => $user->email,
            'password' => $user->password,
        ]);

        $response->assertRedirect(route('user.home'));
        $this->assertAuthenticatedAs($user);
    }

Hi, this is my test.

1 like
dishark's avatar

@PN51291 - You should use the no-hashed version of your password. e.g abc12 intead of $2y$10$JzIpS..NVxH2lkGr3r9n2OxnUL.M96dv6YtbOZF0by3aCm7lqlf5u

pn51291's avatar

Oh, My mistakes. Thanks @dishark and @Sergiu17 for pointing my mistake. I forgot that. However, after I changed the 'password'. the assertAuthenticatedAs return me another error.

It said : The current user is not authenticated. Failed asserting that null is not null.

Sergiu17's avatar

@PN51291 - This is the test :)

// create a factory for user and store it to the database.
$user = factory('App\User')->create([
    // overwrite password field, don't forget to encrypt
    'password' => bcrypt('secret-santa'),
]);

// submit post request to login, without encrypted password
$response = $this->post('/login', [
    'email' => $user->email,
    'password' => 'secret-santa',
]);

// asserts
$response->assertRedirect('/admin');
$this->assertAuthenticatedAs($user);

Adjust your redirect page to whatever you need!

1 like
pn51291's avatar

@SERGIU17 - Thanks for your example, now when I follow your. PHPUNIT return another error current user is not authenticated . I'm so headache with this. I also try to login in browser with same email/password. Everything go smooth, except in PHPUNIT

pn51291's avatar

@SERGIU17 - So headache, maybe my homestead environment don't want to work cause Christmas is very soon.

Thanks so much for your help and time. Merry Christmas @sergiu17 :)

Sergiu17's avatar

@PN51291 - I never used Homestead, not sure if this could be a problem. Here's full class

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function use_can_login()
    {
        $user = factory('App\User')->create([
            'password' => bcrypt('secret-santa'),
        ]);

        $response = $this->post('/login', [
            'email' => $user->email,
            'password' => 'secret-santa',
        ]);

        $response->assertRedirect('/admin');
        $this->assertAuthenticatedAs($user);
    }
}

give a try

shedcollective's avatar

you should post the latest code whenever possible - now we dont know how you changed his example code to suite yours.

pn51291's avatar
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class UserTest extends TestCase
{

    public function test_user_login_with_correct_credentials()
    {
        $user = factory('App\User')->create([
            'password' => bcrypt('123456'),
        ]);
        
        $response = $this->post(route('user.login.submit'), [
            'email' => $user->email,
            'password' => '123456',
        ]);
        
        $response->assertRedirect(route('user.home'));
        $this->assertAuthenticatedAs($user);
    }

}

Hi, this is my test, and this is my error The current user is not authenticated. Failed asserting that null is not null.

If I comment the $this->assertAuthenticatedAs($user); . Everything will good to go. It means I already pass the login, but the assertAuthenticatedAs is NULL

realrandyallen's avatar

@PN51291 - Are you using a custom guard to authenticate your users? assertAuthenticatedAs accepts a second parameter that's a guard name. I'm wondering if you're authenticating successfully but assertAuthenticatedAs is trying to authenticate using a different guard and is failing. Probably unlikely, but it's a thought

Please or to participate in this conversation.