0

I am running unit testing on a Laravel project through PhpStorm inbuilt test environment. I am getting token mismatch exception.

All the configurations are correct and it was working fine. Suddenly this error showed up. I tried with the terminal and it works fine. I am developing this projects under Vagrant.

What can I do to solve this issue?

I googled and found out that changing Laravel's VerifyCsrfToken as below will solve this issue. But I need this to be default as well. $except = ['/*']

2
  • 1
    Try Disabling Middleware in your tests. I woudn't add $except = ['/*'] as it will disable it for all routes... or you could pass the csrf token with the call $this->call('POST', '/your-route', ['_token' => csrf_token(), ...]); Commented Jun 12, 2018 at 7:16
  • @LazyOne - Thanks It worked Commented Jun 14, 2018 at 0:57

2 Answers 2

5

I was experiencing the same problem, i fixed it by setting the "Default configuration file" option in the chosen "Test Frameworks" to the phpunit.xml in my project root. It seems PHPStorm by default uses a 'local' phpunit config. I came to this idea while checking the runningUnitTests() method in Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;

and seeing dat APP_ENV was "local" and not "testing" as specified in my PHPunit.xml

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

I used WithoutMiddleware trait in my testing class. It worked fine. And I tried @Lijubadr 's reply also. It worked too. thank you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.