【发布时间】:2016-11-03 13:53:05
【问题描述】:
这是我要测试的功能:
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 示例的文档也很困难。那里有什么建议吗?感谢您的帮助!
【问题讨论】:
标签: javascript callback promise sinon