samedi 9 novembre 2019

When I run Phpunit with the flag coverage, the error appear: Undefined variable factory

I use the framework Laravel with the package modularization.

I created a module and performed some tests on it. However, I can't run my tests with the coverage (./vendor/bin/phpunit --coverage-text --colors=never). Without the coverage flag, the tests are running correctly (./vendor/bin/phpunit).

here are the two commands executed:

enter image description here

Among all my tests there is one test I do calls the factory

class InstitutionControllerTest extends TestCase
{
    /**
     * @testdox Get the data of institutions
     * @covers \Modules\Institution\Http\Controllers\InstitutionController::index
     * */
    public function testIndex():void
    {
        $institution = factory(Institution::class)->make();
        $institutionCollection = new Collection($institution);
        $mock = Mockery::mock(InstitutionRepository::class);
        $mock->shouldReceive('all')->andReturn($institutionCollection);
        $this->app->instance(InstitutionRepository::class, $mock);
        $response = $this->json('GET', 'api/institutions');
        $this->assertEquals(Response::HTTP_OK, $response->status());
        $this->assertEquals($institutionCollection, $response->getContent());
    }
}

I added the factory to the provide as it is said in the documentation but it doesn't fix the error.

class InstitutionServiceProvider extends ServiceProvider
{
    public function boot()
    {
       ...
        $this->registerFactories();
       ...
    }
    public function register()
    {
        $this->app->register(RouteServiceProvider::class);
        $this->app->singleton(Factory::class, function () {
            $faker = $this->app->make(\Faker\Generator::class);
            return Factory::construct($faker, __DIR__ . '/../Database/factories');
        });
    }
...
    public function registerFactories()
    {
        if (! app()->environment('production') && $this->app->runningInConsole()) {
            app(Factory::class)->load(__DIR__ . '/../Database/factories');
        }
    }
....
}

Do you know how I can solve this error ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire