The issue you're facing is that the Auth facade is not returning the user that was logged in during the test. This is because the Auth facade in tests does not have access to the session data.
To solve this problem, you can manually set the authenticated user in your test using the actingAs method provided by Laravel's testing framework. Here's how you can modify your test to make it work:
it('only shows projects for the authenticated user', function () {
$user = UserFactory::new()->create();
ProjectFactory::new()->user($user)->create();
ProjectFactory::new()->create();
$this->actingAs($user);
$projects = Project::all();
expect($projects)->toHaveCount(1);
});
By using the actingAs method, you are manually setting the authenticated user for the duration of the test. This will ensure that the Auth facade returns the correct user within your model's global scope.
Make sure to import the TestCase class at the top of your test file:
use Tests\TestCase;
I hope this helps! Let me know if you have any further questions.