what does your phpunit.xml look like? Is the APP_ENV set to testing?
Aug 18, 2018
7
Level 12
TokenMismatchException in Unit Tests
I am getting a TokenMismatchException when I try to test a post request.
Laravel should disable csrf when running tests. I've inspected the VerifyCsrfToken's handle method:
public function handle($request, Closure $next)
{
if (
$this->isReading($request) ||
$this->runningUnitTests() ||
$this->inExceptArray($request) ||
$this->tokensMatch($request)
) {
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
Then I checked the runningUnitTests method:
protected function runningUnitTests()
{
return $this->app->runningInConsole() && $this->app->runningUnitTests();
}
and the App::runningUnitTests method:
public function runningUnitTests()
{
return $this['env'] === 'testing';
}
In my tests, when I dump env('APP_ENV') and app()->env I get different values:
APP_ENV is testing but app()->env is local
I believe this is the reason I'm getting this TokenMismatchException but I don't know how to fix it.
Please or to participate in this conversation.