我有一个用于通过会话检查授权的模块。这是一个非常简单的脚本,它会读取会话并在用户登录时返回true或false。这是我的脚本:
module.exports = {
check: function(req) {
return (
req.session &&
req.session.swipe &&
req.session.swipe === true
);
}
}
check()
将请求对象作为参数运行,然后读取其中的会话对象。
我想用Jasmine测试它以确保它返回正确的布尔值。这是规范:
var Auth = require('../controllers/auth');
describe("Authorization", function(){
it("should grant access", function(next) {
var request = {
session: {
swipe: true
}
};
var value = Auth.check(request);
expect(request.session.swipe).toBeDefined();
expect(value).toBe(true);
});
it("should deny access", function(next) {
var request = {
session: {
swipe: false
}
};
var value = Auth.check(request);
expect(request.session.swipe).toBeDefined();
expect(value).toBe(false);
});
});
当它与服务器一起运行时,请求对象是由express生成的,但在测试中我想创建一个仅包含会话变量的模拟。当我运行测试时,jasmine输出以下内容:
我不知道问题在哪里,但我想这是请求对象。
答案 0 :(得分:1)
这个问题有一个简单的解决方案。在传递给它的每个函数的末尾调用next(),或者传递一个没有参数的函数。这将有效:
var Auth = require('./con');
describe("Authorization", function(){
it("should grant access", function(next) {
var request = {
session: {
swipe: true
}
};
var value = Auth.check(request);
expect(request.session.swipe).toBeDefined();
expect(value).toBe(true);
next(); // <----------- added next()
});
it("should deny access", function(next) {
var request = {
session: {
swipe: false
}
};
var value = Auth.check(request);
expect(request.session.swipe).toBeDefined();
expect(value).toBe(false);
next() // <----------- added next()
});
});
以及
var Auth = require('./con');
describe("Authorization", function(){
it("should grant access", function() {// <----------- removed next
var request = {
session: {
swipe: true
}
};
var value = Auth.check(request);
expect(request.session.swipe).toBeDefined();
expect(value).toBe(true);
});
it("should deny access", function() { // <----------- removed next
var request = {
session: {
swipe: false
}
};
var value = Auth.check(request);
expect(request.session.swipe).toBeDefined();
expect(value).toBe(false);
});
});