I’ve constructed a test in which I’m using this setUp() function
public function setUp(): void
{
parent::setUp();
Carbon::setTestNow('2001-01-01');
(new UserFactory())
->for(Company::factory()
->state(
[
'name' => 'test company'
]
)
)
->for(Role::factory()->state([
'name' => 'admin',
'id' => 1,
'hierarchy' => 2
]))
->createOne();
$user = User::first();
$this->actingAs($user);
}
But I’m getting this error: `Error : Class "Database\Factories\CompanyFactory" not found
C:\laragon\bin\php\php-8.1.10-Win32-vs16-x64\php.exe C:/Users/papal/projects/cloud/vendor/phpunit/phpunit/phpunit --configuration C:\Users\papal\projects\cloud\phpunit.xml --filter "/(Tests\\Feature\\Services\\MassiveUploadServicesTest::testGenerateRecipes)( .*)?$/" --test-suffix MassiveUploadServicesTest.php C:\Users\papal\projects\cloud\tests\Feature\Services --teamcity
Testing started at 10:13 ...
PHPUnit 9.6.10 by Sebastian Bergmann and contributors.
Error : Class "Database\Factories\CompanyFactory" not found
C:\Users\papal\projects\cloud\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:821
C:\Users\papal\projects\cloud\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\HasFactory.php:16
C:\Users\papal\projects\cloud\tests\Feature\Services\MassiveUploadServicesTest.php:37
you may think that CompanyFactory.php does not exists but is does, in ...\database\factories\CompanyFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Company>
*/
class CompanyFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'name' => $this->faker->word
];
}
}
What am I missing? This began happening from night to day without any changes in the test. I did a rollback but it’s not working.
>Solution :
If you’ve recently added or moved the CompanyFactory class or made changes to your project’s directory structure, run the following command to regenerate the Composer autoloader:
composer dump-autoload