我在一个控制器中有一个函数调用另一个控制器来获取用户会话。我应该如何使用sinon stub或sinon mock来模拟数据,这样我就可以确保用户controller.js下的Service.USER_ID != uID
##
#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();
});
});