Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Adams_'s avatar

Laravel Feature test: The user is not authenticated Failed asserting that false is true

I am trying to test the user registration, the test file came with laravel jetstream, I just added some additional fields, and I kept getting this error The user is not authenticated Failed asserting that false is true., please anyone with any idea what's wrong should help. User authentication login is working fine but the registration test keep spitting this error

• Tests\Feature\RegistrationTest > new users can register
 The user is not authenticated
 Failed asserting that false is true.

  at C:\wamp64\www\fegocomosa\tests\Feature\RegistrationTest.php:71
  67▕             'password'          => 'password',
  68▕             'password_confirmation' => 'password',
  69▕         ]);
  70▕
  ➜  71▕         $this->assertAuthenticated();
  72▕         $response->assertRedirect('/successful');
  73▕     }
  74▕ }
  75▕

      1   C:\wamp64\www\fegocomosa\vendor\phpunit\phpunit\phpunit:98
        PHPUnit\TextUI\Command::main()

This is the registration test file

 namespace Tests\Feature;

 use Carbon\Carbon;
 use Tests\TestCase;
 use Laravel\Fortify\Features;
 use Laravel\Jetstream\Jetstream;
 use App\Providers\RouteServiceProvider;
 use Illuminate\Foundation\Testing\RefreshDatabase;

 class RegistrationTest extends TestCase
 {
   use RefreshDatabase;

public function test_registration_screen_can_be_rendered()
{
if (! Features::enabled(Features::registration())) {
    return $this->markTestSkipped('Registration support is not enabled.');
}

$response = $this->get('/register');

$response->assertStatus(404);
  }

   public function test_new_users_can_register()
  {
   if (! Features::enabled(Features::registration())) {
    return $this->markTestSkipped('Registration support is not enabled.');
    }

   $response = $this->post('/register', [
    'username'          => 'Test User',
    'first_name'        => 'User',
    'middle_name'       =>  '',
    'last_name'         => 'Username',
    'date_of_birth'     =>  Carbon::parse('1991-04-12'),
    'about_you'         => 'Lorem, ipsum dolor sit amet consectetur adipisicing 
     elit. Quaerat, tempora.',
    'profile_photo_path'=> 'profile-photos/image.jpg',
    'gender_id'         =>  1,
    'marital_status_id' =>  1,
    'address'           => 'new adress',
    'phone'             => '0706074000',
    'state_id'          => 8,
    'city_id'           => 162,
    'profession'        => 'Artisan',
    'workplace'         => 'NOUN',
    'jss_class'         => 'Jss1Q',
    'sss_class'         => 'Sss3Q',
    'house_id'          => 2,
    'potrai_image'     => 'mj_1.jpg',
    'entry_year_id'     => 1,
    'graduation_year_id'=> 2,
    'email'             => '[email protected]',
    'password'          => 'password',
    'password_confirmation' => 'password',
    ]);

    $this->assertAuthenticated();
   $response->assertRedirect('/successful');
  }
}
$response->assertStatus(200);
}

public function test_registration_screen_cannot_be_rendered_if_support_is_disabled()
{
if (Features::enabled(Features::registration())) {
    return $this->markTestSkipped('Registration support is enabled.');
}

$response = $this->get('/register');
0 likes
24 replies
tykus's avatar

Maybe you don't have a successful request; what is the $response status?

$response = $this->post('/register', [
    'username'          => 'Test User',
    // ...
    'password_confirmation' => 'password',
]);

dump($response->getStatusCode());
dump($response->getContent());
Sinnbeck's avatar

So if you go back to how the register action looked at first, it works?

Sinnbeck's avatar

@Adams_ if you set up a new laravel project with Jetstream it still does not work?

Adams_'s avatar

@Sinnbeck I just installed a new laravel project with jetstream and ran the artisan test command, every test that came with jetstream passed except the registration test.

Sinnbeck's avatar

@Adams_ Really strange! I just make a new project and ran tests, and all was successful https://i.imgur.com/JSX2ZR1.png

composer create-project laravel/laravel jet
cd jet
composer require laravel/jetstream
php artisan jetstream:install livewire
//changed both commented lines in phpunit.xml to use sqlite + memory in tests
php artisan test

Is this your approach as well? Tested on php 8.0

Adams_'s avatar

@Sinnbeck exactly the same approach except I didn't uncomment those lines in phpunit.xml to use sqlite + memory, yes I am using php 8.0. Also I am using wamp server

Sinnbeck's avatar

@Adams_ Can you test with sqlite + memory and see if that makes a difference?

Adams_'s avatar

@Sinnbeck So I tested both the new laravel installation and my original project with SQLite + memory and all tests passed but in my original project I still get that error "The user is not authenticated Failed asserting that false is true."

Adams_'s avatar

I did some customization with register response, I thought maybe that was what is interfering with the test causing that error so I revert the to original Register response but still got the same error.

tykus's avatar

@Adams_ does the test actually create a User?

// after creating...
$this->assertDatabaseHas('users', [
    'username' => 'Test User',
]);

You could temporarily disable exception handling to check if there is some exception that is being handled and returning the redirect (which hides the true failure).

public function test_new_users_can_register()
{
    $this->withoutExceptionHandling();
    // ...
Adams_'s avatar

@tykus

Now I got new Exception "The given data was invalid"

        • Tests\Feature\RegistrationTest > new users can register
        PHPUnit\Framework\ExceptionWrapper 

  The given data was invalid.

  at C:\wamp64\www\fegocomosa\vendor\laravel\framework\src\Illuminate\Support\helpers.php:284
    280▕     function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
     281▕     {
    282▕         if ($condition) {
    283▕             if (is_string($exception) && class_exists($exception)) {
  ➜ 284▕                 $exception = new $exception(...$parameters);
    285▕             }
    286▕
    287▕             throw is_string($exception) ? new RuntimeException($exception) : $exception;
    288▕         }

  1   C:\wamp64\www\fegocomosa\vendor\phpunit\phpunit\phpunit:98
    PHPUnit\TextUI\Command::main()


  Tests:  1 failed, 1 skipped, 30 passed
  Time:   20.62s
martinbean's avatar
Level 80

@Adams_ So you have validation failures, hence the request wasn’t successful and hence why you’re not authenticated.

You can assert the request was successful and didn’t throw any validation errors by using $response->assertValid() or $response->assertSessionHasNoErrors().

https://laravel.com/docs/9.x/http-tests#response-assertions

So, execute the request and then assert it was successful before checking anything else:

$response = $this->post('/register', [
    // Your shit-ton of fields here
]);

// Check the request was successful
// If any validation errors occurred, they will be printed here
$response->assertValid();

// Now check the user is authenticated after you've checked the request was actually successful
$this->assertAuthenticated();
1 like
tykus's avatar

@Adams_ this is good, progress at last. Fix the validation issues, and work on from there. The withoutExceptionHandling is an invaluable tool for debugging your test flow (they're not always right the first time).

1 like
Adams_'s avatar

@martinbean U are right I do have validation errors, that's what's causing the test failure, now it all passed. Thank you very much.

1 like
Adams_'s avatar

@tykus Thank you, absolutely the without exception handling method helps in uncovering other hidden exceptions...

Adams_'s avatar

@tykus You know if not for people like you and @martinbean and @sinnbeck at laracast I would have given up on this journey to become a "self-thought developer", is not easy at all, sometimes I spend weeks or days trying to debug, I come here every time it became clear to me that I can't do it, I need to ask for help and thank God for Laracast community there is always someone ready to help. Thank you all I appreciate it.

1 like

Please or to participate in this conversation.