Hey @andreas.loew, can you post the code for hasProject() and the code that runs when the form on /new-project is run? Also, just curious, what is the CACHE_DRIVER in your phpunit.xml file set to?
Cached user model lets my test cases fail
Scenario:
A user has to own a project to work with my web app. I am trying using a middleware to enforce the existing of a project:
class VerifyUserHasProject
{
public function handle($request, Closure $next)
{
if ($request->user() && ! $request->user()->hasProject()) {
return redirect('new-project');
}
return $next($request);
}
}
Creating a project is some kind of wizard. So after submitting to /new-project the user should be taken to a second page with more project details: /new-project/select-type.
This all works well manually.
I've also added a test:
public function user_can_create_new_project()
{
$user = factory(App\User::class)->create();
$this
->actingAs($user)
->visit("/new-project")
->type("project1", "name")
->press('Create')
->seeInDatabase("projects", ["name" => "project1", "owner_id" => $user->id])
->seeStatusCode(200)
->seePageIs("/new-project/select-type") // this line fails!
;
}
This test fails because the resulting page is /new-project and not /new-project/select-type.
But: Everything else is ok, the project is created and available in the database.
The problem is that the user model is not reloaded in the middleware and still thinks that the user has no projects.
Making the following change to the middleware solved the issue:
if ($request->user() && ! $request->user()->hasProject()) {
to
if ($request->user() && ! $request->user()->fresh()->hasProject()) {
But this is not what I want: I don't want to add the reload of the user model in the middleware just to make a test pass.
Is there a way to disable caching the user model in the test?
Please or to participate in this conversation.