Antonella's avatar

TDD: Illuminate\Session\TokenMismatchException: CSRF token mismatch.

I'm at this lesson https://laracasts.com/series/build-a-laravel-app-with-tdd/episodes/16 minute 11:00

when i launch this test i get a different error than the course one

public function test_a_user_can_update_a_project()
{
    $this->signIn();

    $this->withoutExceptionHandling();

    $project = Project::factory()->create(['owner_id'=>auth()->id()]);

    $this->patch($project->path(),[
        'notes'=>'Changed'
    ]);

    $this->assertDatabaseHas('projects',['notes'=>'Changed']);
}

gives me following errors:

  1. Tests\Feature\ManageProjectsTest::test_a_user_can_update_a_project Illuminate\Session\TokenMismatchException: CSRF token mismatch.

this is my route web:

Route::group(['middleware'=>'auth'],function(){
Route::get('/projects', [ProjectsController::class, 'index']);
Route::get('/projects/create', [ProjectsController::class, 'create']);

Route::get('/projects/{project}', [ProjectsController::class, 'show']);
Route::patch('/projects/{project}', [ProjectsController::class, 'update']);

Route::post('/projects', [ProjectsController::class, 'store']);
Route::post('/projects/{project}/tasks', [ProjectTasksController::class, 'store']);
Route::patch('/projects/{project}/tasks/{task}', [ProjectTasksController::class, 'update']);

});
0 likes
5 replies
Antonella's avatar

yes

<php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
     <server name="DB_CONNECTION" value="sqlite"/>
     <server name="DB_DATABASE" value=":memory:"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
</php>
Antonella's avatar
Antonella
OP
Best Answer
Level 6

i solved with

 php artisan config:clear
6 likes
quyle's avatar

@Antonella thanks. This works... But I don't know if I run "php artisan config:cache" or "php artisan config:clear" did not solve the problem? as when doing unit testing, I did not tamper with any of the config files so why clear config cache works but not app cache?

lemmon's avatar

@gianmarx I have had this same problem for a month where this would happen off and on using wamp and your solution solved this. I am going to look for my old post and refer them to this post

Please or to participate in this conversation.