在单元测试嘲讽Passport.js(本地策略) [英] Mocking Passport.js (local strategy) in a unit test

查看:86
本文介绍了在单元测试嘲讽Passport.js(本地策略)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Passport.js的地方战略,为我的API认证用户。我写测试这个API,我需要在测试中嘲讽Passport.js模拟一个身份验证/授权的用户击中API的方式。

I am using the local strategy of Passport.js to authenticate users for my API. I am writing tests for this API, and I need a way of mocking Passport.js in the tests to simulate an authenticated/unauthenticated user hitting the API.

什么是这样做的最有效的方法是什么?

What would be the most efficient way of doing this?

推荐答案

一个可能的选项:在您的测试设置,您可以 passport.use 在当地一个模拟策略名称:

One possible option: in your test setup, you can passport.use a mock strategy under the local name:


passport.use(本地,新MockStrategy());

实施MockStrategy将请求传递,或任何你的测试需求。

Implement MockStrategy to pass the request, or whatever your test needs.

这篇关于在单元测试嘲讽Passport.js(本地策略)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆