运行测试时功能未触发

时间:2019-11-19 07:54:33

标签: laravel testing

我是Laravel的新手,所以我对自己所做的事情只有一个模糊的想法。我正在执行功能测试和一个我知道的功能,当我使用邮递员测试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');
        }
    }


这是路由控制器指向的功能的代码

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字段不应该返回空值。

奇怪的是,有一个$ response-> assertOk();并且只有在markEmailAsVerified()函数成功触发后,响应才可以,否则响应将不是代码200。markEmailAsVerified()函数正在执行预期的操作,因为当我在被注释的测试中自行调用时出来,测试又回来了。

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

1 个答案:

答案 0 :(得分:1)

尝试获取您的用户的新实例吗?

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