我是 Laravel 模型单元测试的新手。所以请看看并建议我做错了什么。我的代码如下。我有 2 个模型 User 和 UserState。
模型用户
public function state()
{
return $this->hasOne('UserState');
}
Run Code Online (Sandbox Code Playgroud)
模型用户状态
public function user()
{
return $this->belongsTo('User');
}
Run Code Online (Sandbox Code Playgroud)
现在我正在为 UserState 编写单元测试。下面给出:
单元测试 UserStateModelTest
public function testUserRelationIsTrue(){
$user = new User();
$user->username = 'testusername';
$user->save();
$this->assertEquals($user->user_id, $user->state->id);
}
Run Code Online (Sandbox Code Playgroud)
在 phpunit 测试运行期间,它生成错误
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation:
1452 Cannot add or update a child row: a foreign key constraint fails
Run Code Online (Sandbox Code Playgroud)