Hi, would be nice to see your test
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 :)
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.
@PN51291 - What is $user->password equals to?
@PN51291 - You should use the no-hashed version of your password. e.g abc12 intead of $2y$10$JzIpS..NVxH2lkGr3r9n2OxnUL.M96dv6YtbOZF0by3aCm7lqlf5u
@DISHARK - No!
@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!
@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 - Works on my machine, lol

@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
you should post the latest code whenever possible - now we dont know how you changed his example code to suite yours.
<?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
@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.