0
我试图测试一个函数,读取文件并返回一个承诺与文件的内容。测试js承诺与摩卡,柴,chaiAsPromised和Sinon
function fileContents(){
return new Promise(function(resolve, reject) {
fs.readFile(filename, function(err, data){
if (err) { reject(err); }
else { resolve(data); }
});
});
}
用于上述
describe('Testing fileContents', function() {
afterEach(function() {
fs.readFile.restore();
});
it('should return the contents of the fallBack file', function() {
let fileContents = '<div class="some-class">some text</div>';
sinon.stub(fs, 'readFile').returns(function(path, callback) {
callback(null, fileContents);
});
let fileContentsPromise = fileContents();
return fileContentsPromise
.then(data => {
expect(data).to.eventually.equal(fileContents);
});
});
上述测试单元测试犯错与
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我还试图
describe('Testing fileContents', function() {
afterEach(function() {
fs.readFile.restore();
});
it('should return the contents of the fallBack file', function (done) {
let fileContents = '<div class="some-class">some text</div>';
sinon.stub(fs, 'readFile').returns(function(path, callback) {
callback(null, fileContents);
});
let fileContentsPromise = fileContents();
fileContentsPromise.then(function(data){
expect(data).to.equal(fileContents);
done();
});
});
,并得到了同样的错误。该函数在我的本地站点工作,但我不知道如何为它编写测试。我对js很陌生。我错过了什么?
产量正是我正在寻找,非常感谢你。对于有变数名称的红鲱鱼,我的原始代码中没有;我已经将代码更改为更通用的代码,并没有注意到我已经为'假内容'和函数名称使用了相同的名称。无论如何,再次感谢! – margo