如何在 Promise 中实现回调以使用 Mocha 和 Sinon 进行测试?

How do I reach a callback within a Promise for testing using Mocha and Sinon?

这是我要测试的函数:

write(filename, content, theme) {
    return new Promise((resolve, reject) => {
        let filePath = path.resolve(this.config.output.path, this.defineFilename(filename, theme, content));
        fs.writeFile(filePath, content, e => {
            if (e != null) reject(e);
            this.addToManifest(filename, filePath, theme);
            resolve(filePath);
        });
    });
}

这是我目前所了解的内容,但我的理解中遗漏了一些内容。我不明白在我的测试中要在回调中放什么。

        it('should reject the promise if there is an error', () => {
            const sandbox = sinon.sandbox.create();
            var expectedError = function(e) {

            };
            sandbox.stub(fs, 'writeFile', (filePath, content, expectedError) => {

            });

            sandbox.stub.onCall(0).returns('Hello');

            return instance.write(filename, content, theme).then((data) => {
                console.log('should not get to this line');
                expect(data).to.not.be.ok;
                sandbox.restore();
            }).catch((err) => {
                expect(err).to.be.an('error');
                sandbox.restore();
            });
        });

查找包含 sinon.js 示例的文档也具有挑战性。有什么建议吗?感谢您的帮助!

我想,为了模拟writeFile的错误,应该是:

sandbox.stub(fs, 'writeFile', (filePath, content, callback) => {
    callback(new Error())
});