- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在学习测试 jwt 身份验证快速中间件。我的下一个回调正在被调用,因为我在那里放置了一个 console.log
但我的 sinon spy 断言失败了。
有人可以看一下这个案例吗?
这是我的测试用例
it("should call next when the token provided is valid", () => {
let token = jwt.sign({}, process.env.JWT);
let request = httpMocks.createRequest({
headers: {
Authorization: `Bearer ${token}`
}
});
const next = sinon.spy();
authenticateJwt(request, response, next);
expect(next.calledOnce).to.be.true;
});
这是我的中间件
import jwt from "jsonwebtoken";
export default function(req, res, next){
const authorizationHeaders = req.headers["authorization"];
let token;
if(authorizationHeaders){
token = authorizationHeaders.split(" ")[1];
}
if(token){
jwt.verify(token, process.env.JWT, (err, decodedToken) => {
if(err){
res.status(401).json({
message: "invalid token provided"
});
} else {
res.user = decodedToken;
console.log("called");
next();
}
});
} else {
res.status(401).json({
success: false,
message: "no token provided"
});
}
}
我的 console.log
正在正确记录,但 sinon 断言失败。
最佳答案
您的期望
可能发生得太早了。在断言回调已经发生之前,您不会等待回调发生。
在此示例中,您可能根本不需要 sinon,这应该适合您:
it("should call next when the token provided is valid", () => {
let token = jwt.sign({}, process.env.JWT);
let request = httpMocks.createRequest({
headers: {
Authorization: `Bearer ${token}`
}
});
return authenticateJwt(request, response, () => {
// Do assertions in here
});
});
也许更好的格式化方法是:
describe('authenticateJwt middleware', () => {
let nextCalled = false;
before(() => {
let token = jwt.sign({}, process.env.JWT);
let request = httpMocks.createRequest({
headers: {
Authorization: `Bearer ${token}`
}
});
return authenticateJwt(request, response, () => {
nextCalled = true;
});
})
it("should call next when the token provided is valid", () => expect(nextCalled).to.be.true);
});
这使您的断言更加灵活。如果由于某种原因未调用它,它还确保它会失败。
关于node.js - 在快速中间件内没有调用Sinon spy ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42782523/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!