我正在尝试测试一个返回承诺的方法调用,但是我遇到了麻烦。这在 NodeJS 代码中,我使用 Mocha、Chai 和 Sinon 来运行测试。我目前的测试是:
it('should execute promise\'s success callback', function() {
var successSpy = sinon.spy();
mySpies.executeQuery = sinon.stub(databaseConnection, 'execute').returns(q.resolve('[{"id":2}]'));
databaseConnection.execute('SELECT 2 as id FROM Users ORDER BY RAND() LIMIT 1').then(successSpy, function(){});
chai.expect(successSpy).to.be.calledOnce;
databaseConnection.execute.restore();
});
但是,此测试出错:
AssertionError: expected spy to have been called exactly once, but it was called 0 times
测试返回承诺的方法的正确方法是什么?