繁体   English   中英

如何对 cookie 是否在 Laravel 中排队或设置进行单元测试

[英]How to unit test if the cookie was queued or set in Laravel

我正在开发 Laravel 应用程序。 我正在为我的应用程序编写集成测试。 我正在努力测试 Cookie 逻辑。 你可以在下面看到我的代码。

我正在覆盖 LoginController 的登录方法,添加以下位

if (auth()->user()->hasRole(User::ROLE_ADMIN)) {
                $accessToken = GraphQL::login($request->get('email'), $request->get('password'));
                Cookie::queue("admin_access_token", $accessToken, (60 * 60 * 24) * 60);
            }

如您所见,我想要做的是,如果用户是管理员,它将登录到另一台服务器并将访问令牌保存在 cookie 中。 我正在使用 Cookie::queue。

这是我的测试方法。

/** @test */
    public function when_login_successful_access_token_is_generated_and_stored_in_cookie_if_user_is_restaurant_admin()
    {
        $this->post(route('login'), [
            'email' => $this->restaurantAdmin->email,
            'password' => 'password'
        ])->assertRedirect();

        dd(Cookie::get('admin_access_token'));//here, coolie value is always null
    }

当我运行测试时,正如您在评论中看到的那样,cookie 值始终为 null。 我的代码有什么问题,我该如何解决?

我会假设 static Cookie::get()会尝试在调用的请求生命周期中获取 cookie。 由于在测试中没有,cookie 将始终为 null。

有一种方法可以查看响应中是否存在 cookie,您可以使用它并查看它是否给出了预期的结果。

$this->seeCookie('admin_access_token', 'yourtoken');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM