我在一个控制器中有一个函数调用另一个控制器来获取用户会话。我应该如何使用 sinon stub 或 sinon mock 来模拟数据,这样我才能确保Service.USER_ID != uID
用户 controller.js
##
#user controller js
##
const limitConst = sails.config.globals.limitConst;
const checkuserID = function (uID, cb) {
if (uID === Service.USER_ID) {
return getUserPermission(cb);
}
}
const getUserPermission = function (cb) {
User.find({ select: ['id'], where: { enabled: 1 } }).limit(limitConst).exec(function (err, memberIds) {
return cb(null, memberIds);
});
});
};
##
#Service controller js
##
const USER_ID = 0;
const getUserPermission = function (session) {
return _.get(session, 'user.id', USER_ID);
};
##
#test case js
##
it('Should get session.', function (done) {
request(sails.hooks.http.app)
.get('user/getuserpermission')
.set('Authorization', 'oasis')
.send('Accept', 'application/json')
.send(data)
.end(function (err, res) {
done();
});
});