所以我尝试在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]),
];
}
}
模型\项目我刚刚使用了fillable
<?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);
}
}
请帮帮我我快死了
1条答案
按热度按时间ijxebb2r1#
任何人想知道如何修复这个错误,我随机修复了它,在我的测试类中替换它。
具有以下
我是通过引用Code with Dary的一段视频做到这一点的