Lumen-framework: [5.7] Tests using the be() and/or actingAs() helpers fail

Created on 14 Sep 2018  路  5Comments  路  Source: laravel/lumen-framework

  • Lumen Version: 5.7.0
  • PHP Version: 7.2

Description:

Tests using the be() and/or actingAs() helpers but not making an actual request fail with the following message:

InvalidArgumentException: Auth driver [api] for guard [api] is not defined.

After investigating the issue, I concluded that it's due to the newest changes introduced in the Application class which require the application to be explicitly booted using the boot() method which is only called from within the dispatch method of the RoutesRequests trait.

I would submit a patch for this but I'm not sure on where to call the boot method for a testing scenario. It seems like a good place do to so would be the refreshApplication() method of the TestCase class.

Steps To Reproduce:

  1. Create a new test.
<?php

use Illuminate\Auth\GenericUser;
use Test\TestCase;

class AuthTest extends TestCase
{
    public function setUp()
    {
        parent::setUp();

        $user = new GenericUser([
            'id' => 1
        ]);

        $this->be($user);
    }

    public function testAuth()
    {
         $this->assertTrue(true);
    }
}
  1. Run phpunit

All 5 comments

Did you also encounter #806? I had that issue first, and then after my workaround, this one happened.

I don't have any tests involving models, I would do a quick one to check but am not currently at my desk, if you could, go to your local TestCase class override the refreshApplication method, call parent::refreshApplication() and under that add a call to $this->app->boot(); and see if that works for both cases and without your workaround.

@stefpankov I can confirm that workaround indeed works.

I made a pull request to fix this issue.

Fixed in #809

Was this page helpful?
0 / 5 - 0 ratings