私はファイルを読み込み、ファイルの内容を使って約束を返します。mocha、chai、chaiAsPromisedとSinonとのjs約束事をテストする
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.
とERRS Iはまた
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が初めてです。私は何が欠けていますか?
収量が正確に何をされるI探していた、ありがとう。私の元のコードではない変数名を持つ赤いニシンの謝罪。より一般的なコードに変更し、「偽のコンテンツ」と関数名に同じ名前を使用していることに気付かなかった。とにかく、もう一度ありがとう! – margo