Mokeyjay's Blog Mokeyjay's Blog -

Laravel 功能测试中设置请求 Cookie 的方法

TL;DR $this->withUnencryptedCookie('cookie_name', $cookieValue) ->withCredentials() 正文 在功能测试中,我们通常要使用 $this->postJson() 系列方法来测试接口,在此之前只要用 $this->actingAs() 即可解决登录态的问题 不过这次要测的接口比较特别,需要往 coo The post Laravel 功能测试中设置请求 Cookie 的方法 first appeared on mokeyjay - 超能小紫.

在功能测试中,使用 $this->postJson() 方法测试接口时,需要往 cookie 中塞 token 来维持登录态。通过设置 $this->withCredentials 属性为 true,可以解决这个问题。

$this->postJson() $this->withCredentials cookie token 功能测试

相关推荐 去reddit讨论