Laravel特性测试匹配动态值

t5fffqht  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(96)

我在Laravel功能测试中声明JSON response

    • 答复**
{
    "message": "OTP verified successfully",
    "phone": "+12312322334",
    "route": "dashboard",
    "_token": "Some Random Value of Token",
    "id": 9
}

这里_token是动态的,我不知道确切的结果。那么如何处理_token字段的随机字符串呢?

$response->assertStatus(200)->assertJson([
    "message" => "OTP verified successfully",
    "phone" => "+12312322334",
    "route" => "dashboard",
    "_token" => "how to handle here the random dynamic value",
    "id" => 9,
]);
hmae6n7t

hmae6n7t1#

因为_token这里是一个随机的string和未知的随机值,所以这里的情况是,我们需要检查该值是string数据类型。

$response->assertStatus(200)
            ->assertJson(
                fn (AssertableJson $json) =>
                $json->where('message', "OTP verified successfully")
                    ->where('phone', "9953029043")
                    ->where('route', "dashboard")
                    ->whereType('_token', 'string')
                    ->where('id', 9)
            );

相关问题