所以我尝试在 Laravel 中使用工厂测试 我已经收到这个错误很长时间了,这很烦人 我正在尝试使用 faker 进行测试,检查它是否创建数据并将其插入到 数据库。
Target class [config] does not exist.
这是我的测试课
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Project;
class CreateProjectTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
public function test_createproject()
{
$project = Project::factory()->create();
$title = $project -> title;
$this -> assertNotEmpty($title);
}
}
项目工厂
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Project>
*/
class ProjectFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => fake() -> name(),
'description' => fake() -> sentence,
'deadline' => fake() -> date('Y_m_d'),
'status' => fake() -> randomElement(['Proposal', 'In Progress', 'Completed']),
'client_id' => fake() -> randomElement([2, 3, 5, 8, 9, 13, 16, 17]),
];
}
}
模型\项目 我刚刚使用了可填充
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;
class Project extends Model
{
use HasFactory;
protected $guarded = [];
protected $fillable = [
'title',
'description',
'deadline',
'status',
'client_id',
];
public function client()
{
return $this->belongsTo(User::class, 'client_id', 'id');
}
public function userProjects(){
return $this-> belongsToMany(User::class)->where('is_client',0);
}
}
请帮助,这现在快要了我的命
任何人想知道如何修复这个错误,我通过在我的测试类中替换它来随机修复它
use PHPUnit\Framework\TestCase;
具有以下内容
use Tests\TestCase;
我通过参考 Code with Dary 的视频来做到这一点