When running phpunit test following Error occurs in Building Laravel App with TDD episode 5 vendor/bin/phpunit tests/Feature/ProjectsTest.php
PHPUnit 7.5.14 by Sebastian Bergmann and contributors.
E.EFF 5 / 5 (100%)
Time: 17.61 seconds, Memory: 22.00 MB
There were 2 errors:
Tests\Feature\ProjectsTest::a_user_can_create_a_project
Illuminate\Session\TokenMismatchException: CSRF token mismatch.
Add the following in the function to disable csrf:
$this->withoutMiddleware(VerifyCsrfToken::class);
Can you show the code for a_user_can_create_a_project test case?
It should not be necessary explicitly disable the VerifyCsrfToken middleware; this is handled automatically whenever the environment is testing; which is set by the APP_ENV environment setting in the phpunit.xml file in the project root. I am guessing that your PHPUnit is not using this configuration - are you running the tests in PHPStorm; can you check the Default Configuration for PHPUnit?
I'm using VS Code .In Phpunit xml file it is configured to testing.
./tests/Unit
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="mysql"/>
<server name="DB_DATABASE" value="birdboardtest"/>
<server name="DB_HOST" value="127.0.0.1"/>
<server name="DB_PORT" value="3306"/>
<server name="DB_USERNAME" value="root"/>
<server name="DB_PASSWORD" value="1234"/>
</php>
/** @test */
public function a_user_can_create_a_project()
{
$this->withoutExceptionHandling();
$this->withoutMiddleware(VerifyCsrfToken::class);
$attributes = [
'title' => $this->faker->sentence,
'description' => $this->faker->paragraph
];
$this->post('/projects',$attributes);
$this->assertDatabaseHas('projects',$attributes);
}
Please sign in or create an account to participate in this conversation.