Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel – Why is my test setUp() function failing?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading