gpt4 book ai didi

laravel - 运行测试时功能未触发

转载 作者:行者123 更新时间:2023-11-28 21:15:34 25 4
gpt4 key购买 nike

我是 Laravel 的新手,所以我对自己在做什么只有一个模糊的概念。我正在做一个功能测试和一个函数,我知道当我使用 postman 测试 api 时会触发,但在测试期间不会。这是测试

public function testVerify(){
$this->createTestUserParams();
$response = $this->post(route('register'), $this->user_params);
$response->assertOk();
$user = User::where('email','test@gmail.com')->first();
if($user){
$token = $user->verifyUser->token;
$id = $user->verifyUser->user_id;
$response2 = $this->post(route('email.customVerify'), ['user_id' => $id, 'token' => $token]);
$response2->assertOk();

//$user->markEmailAsVerified();
$this->assertNotNull($user->email_verified_at);
}else{
$this->fail('should find a user');
}
}


这是路由 Controller 指向的函数的代码

public function customVerify(Request $request){
if(!isset($request->user_id)){
return response()->json(['message' => 'No user ID'],400);
}
if(!isset($request->token)){
return response()->json(['message' => 'No user token'],400);
}
$user = User::where('id',$request->user_id)->first();

if($user == null){
return response()->json(['message' => 'Bad User Id'],400);
}
if ($user->hasVerifiedEmail()) {
return response()->json(['message' => 'Already verified'],400);
}

if($request->token == $user->verifyUser->token){
if($user->markEmailAsVerified()){
event(new Verified($user));
VerifyUser::where('user_id',$user->verifyUser->user_id)->first()->delete();
return response()->json(['message' => 'Everything is swell'],200);

}
}else{
return response()->json(['message' => 'Bad token'],400);

}
}

我的问题是 email_verified_at 字段不应该返回 null。

奇怪的是有一个 $response->assertOk();只有当 markEmailAsVerified() 函数成功触发时,响应才会正常,否则响应将不是代码 200。并且 markEmailAsVerified() 函数正在做它应该做的事情,因为当我在它被注释的测试中自己调用它时出来,测试回来很好。

如果有帮助,我正在使用护照库进行身份验证。

最佳答案

尝试获取用户的新实例?

$user = $user->fresh();
$this->assertNotNull($user->email_verified_at);

关于laravel - 运行测试时功能未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com