У меня есть функция isMember, как показано ниже;Шпион не мог отслеживать при проверке функции Async с помощью Mocha и Sinon
function isMember(req, res, next) {
MyService.GetUserAsync(authCookie)
.then(function (user) {
next();
})
.catch(function (err) {
if (err.status === 400) {
return res.redirect("/notAllowed");
}
else {
return next(err);
}
});
}
Мой тест, как показано ниже;
beforeEach(function() {
// Overwrite the global timer functions (setTimeout, setInterval) with Sinon fakes
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
// Restore the global timer functions to their native implementations
this.clock.restore();
});
it.only("pass authentication and set authCookie", function (done) {
var user = {
userNameField: "fakeUserName"
};
sinon.stub(MyService, "GetUserAsync").returns(Promise.resolve(user));
var spyCallback = sinon.spy();
var req {};
var res = {};
isMember(req, res, spyCallback);
// Not waiting here!
this.clock.tick(1510);
// spyCallback.called is always false
expect(spyCallback.called).to.equal(true);
done();
});
По какой-то причине this.clock.tick
не работает и spyCallback.called
всегда ложно. Как я могу добиться того, что мой шпион будет ждать, пока next()
вызывается в функции isMember?
Правильно, но функция isMember не возвращает обещание по дизайну. Это файл политики sails.js. –
Затем вы можете создать свой собственный обратный вызов, используя isMember (req, res, function() {next()}) –