首页 > 解决方案 > 测试“工厂”未定义

问题描述

嗨,我正在尝试实施 test_user_can_login_with_correct_credentials 测试,但我无法弄清楚为什么会出现“工厂”未定义错误。

我在网上搜索过,人们似乎得到了这个,因为他们没有导入用户模型,但我有

这是代码

<?php

namespace Tests\Feature;



use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;


class LoginTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_user_can_view_a_login_form()
    {
        $response = $this->get('/login');

        $response->assertSuccessful();
        $response->assertViewIs('auth.login');
    }

    public function test_user_can_login_with_correct_credentials()
    {
       $user = factory(User::class)->create([ // Error with 'factory' on this line
           'first_name' =>('John'),
           'last_name' => ('Smith'),
           'email' => ('john@example.com'),
           'date_of_birth' => ('16/02/2001'),
           'password' => Hash::make('test'),
           'join_date' => Carbon::now()
       ]);

       $response = $this->from('/login')->post('/login', [
           'email' => $user->email,
           'password' => 'test',

       ]);
        $response->assertRedirect('/login');
        $response->assertSessionHasErrors('email');
        $this->assertTrue(session()->hasOldInput('email'));
        $this->assertFalse(session()->hasOldInput('password'));
        $this->assertGuest();
    }
}

确切的错误是'未定义的函数'Tests\Feature\factory'.intelephense(1010)'

任何帮助将不胜感激我不确定如何解决这个问题

标签: laravelunit-testingphpunit

解决方案


推荐阅读