2
votes

I am getting this error in my log file when I run codeception test case. How to fix this?

Code:

 public function testUpdatePhone(FunctionalTester $I)
 {
        $I->wantTo('Test update phone');
        $I->sendPUT('/admin/phone/100', [
            'label' => 'My new label'
        ]);
        $I->see('Phone Number updated successfully.');
 }

Command:

./vendor/bin/codecept run tests/functional/AdminPhoneTestCest.php:testUpdatePhone

Error in Log file:

[2015-06-06 05:34:02] local.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /var/www/xxxx/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:46

Thanks in advance :)

2
The issue is that the data which is being passed to that test route does not include a valid Laravel csfr_token() stored in a _token form field which the middleware in your error is expecting. This field has to be generated for each request, or it will fail authentication. You'll want to search for a work around. - Azeame

2 Answers

1
votes

This issue has been fixed by adding middleware method as follows

public function handle($request, Closure $next)
{
 if ($request->header('user-agent') == 'Symfony2 BrowserKit') {
  return $next($request);
 }

 throw new TokenMismatchException;
}

Reference: https://laracasts.com/discuss/channels/general-discussion/latest-v5-laravelframework-csrfmiddleware-changes-broke-codeception-functional-tests

0
votes

If you're using phantomjs, you will need to ensure you have clear_cookies: true in your settings as the normal restart: true doesn't seem to be supported for phantomjs, so it uses the same token which is invalid in subsequent tests.