laravel-modules: Undefined variable: factory

When I run my tests with this command ./vendor/bin/phpunit --coverage-text --colors=never I have this error: Undefined variable: factory.

I have checked that the registerFactories() method is called in my provider (MyModuleServiceProvider), it is well called.

I saw on the documentation that we need to add this piece of code in MyModuleServiceProvider

$this->app->singleton(Factory::class, function () {
            $faker = $this->app->make(\Faker\Generator::class);
            return Factory::construct($faker, __DIR__ . '/../Database/factories');
});

it doesn’t work ( same after I ran the command composer dumpautoload )

Do you know how to fix the error ?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

I simply added an exclusion of my factories from file who is covered by Phpunit and this fixed my problem. I hope this will fix your problem too.

phpunit.xml

        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./src</directory>
            <exclude>
                <directory>src/Database/Factories</directory>
            </exclude>
        </whitelist>