Laravel/PHPUnit Testing – TokenMismatchException

Sometimes when running our tests we find a weird «CSRF token mismatch» error

That is because in phpunit.xml we partially override the default enviroment:

<env name="APP_ENV" value="testing" />

However, if config is cached, this value won’t be passed to it. In VerifyCsrfToken app env is checked. If enviroment is testing, and request come from console token isn’t required.

So for a fast solution, simply run from console:

php artisan config:clear