zoukankan      html  css  js  c++  java
  • Factory can't be seen in unit test.

    Factory can't be seen in unit test.

    This is how my ModelFactory.php looks:

    <?php
    
    /** @var IlluminateDatabaseEloquentFactory $factory */
    
    use AppUser;
    use FakerGenerator as Faker;
    use IlluminateSupportStr;
    
    $factory->define(User::class, function (Faker $faker) {
        return [
            'name' => $faker->name,
            'email' => $faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    });
    
    $factory->define(AppCourse::class, function (Faker $faker) {
        return [
            'user_id' => function () {
                return (factory(User::class)->create())->id;
            },
            'name' => $faker->sentence,
            'short_description' => $faker->sentence(20),
            'description' => $faker->paragraph(10),
            'seats' => random_int(0, 10),
            'expiry_date' => $faker->dateTimeBetween('+0 days', '+3 months'),
        ];
    });
    

    And this is how my unit test looks:

    <?php
    
    namespace TestsUnit;
    
    use IlluminateFoundationTestingRefreshDatabase;
    use PHPUnitFrameworkTestCase;
    
    class CourseTest extends TestCase
    {
        use RefreshDatabase;
    
        /** @test */
        public function a_course_belongs_to_a_teacher() {
            $course = factory(AppCourse::class, 1)->create();
    
            //$this->assertInstanceOf(AppCourse::class, $course->teacher);
        }
    }
    

    This is the error I receive InvalidArgumentException: Unable to locate factory with name [default] [AppCourse].

    I can't understand why, any help is appreciated.

    Nakov
    Level 50
    Nakov3 months ago
     

    @catalinul Ah, I see I believe you have the wrong import for the parent class.

    Use this

    use TestsTestCase;
    

    instead of

    use PHPUnitFrameworkTestCase;
    

    as the first one is where Laravel does all it's startup.

  • 相关阅读:
    CodeForces 347B Fixed Points (水题)
    CodeForces 347A Difference Row (水题)
    CodeForces 346A Alice and Bob (数学最大公约数)
    CodeForces 474C Captain Marmot (数学,旋转,暴力)
    CodeForces 474B Worms (水题,二分)
    CodeForces 474A Keyboard (水题)
    压力测试学习(一)
    算法学习(一)五个常用算法概念了解
    C#语言规范
    异常System.Threading.Thread.AbortInternal
  • 原文地址:https://www.cnblogs.com/mouseleo/p/12693817.html
Copyright © 2011-2022 走看看