我有一個從目錄中解壓縮檔案的功能。它作業正常
索引.js
const unZip = async (zipFilePath, destDir) => {
await util.promisify(fs.mkdir)(destDir);
return new Promise((resolve, reject) => {
fs.createReadStream(zipFilePath)
.pipe(unzipper.Extract({ path: destDir }))
.on("close", () => resolve(destDir))
.on("error", (err) => {
console.log("Error inside unzip", err);
reject(err);
});
});
};
但是對于我正在使用的單元測驗sinon和ava,我無法通過測驗用例的地方是代碼
index.test.js
ava.beforeEach(() => {
// mockFs({
// 'fakeDir/fakeFile': mockFs.load('test/helpers/file/testFile.txt'),
// fakeFileContent: 'content here',
// });
sinon.stub(mockFs, 'createReadStream').returns({
pipe: sinon.stub().returns({
on: sinon.stub().returns({
on: sinon.stub().returns(),
}),
}),
});
});
ava.serial('unZip test', async (t) => {
const unzip = proxyquire('../../../src/helpers/file/unZip', {
fs: mockFs,
util: {},
unzipper: { Extract: () => Buffer.from([8, 6, 7, 5, 3, 0, 9]) },
});
const result = await unzip('fakeFileContent', 'fakeFileContent');
t.is(result, true);
});
它給了我這樣的錯誤
unZip test
Rejected promise returned by test. Reason:
Error {
code: 'EEXIST',
errno: -17,
path: 'fakeFileContent',
syscall: 'mkdir',
message: 'EEXIST: file already exists, mkdir \'fakeFileContent\'',
}
uj5u.com熱心網友回復:
您不需要使用proxyquire包,用于sinon.stub(obj, 'method')存根物件的方法。你可以存根fs.mkdir,unzipper.Extract和fs.createReadStream方法。
使用util.promisifyto轉換fs.mkdir成promise形式并呼叫,但底層仍然是回呼被呼叫,所以需要使用.callsFake()方法來mock實作for fs.mkdir,在測驗用例中手動呼叫回呼。
下面的示例mocha用作測驗框架,但ava也應該沒問題。
index.js:
const fs = require('fs');
const util = require('util');
const unzipper = require('unzipper');
const unZip = async (zipFilePath, destDir) => {
await util.promisify(fs.mkdir)(destDir);
return new Promise((resolve, reject) => {
fs.createReadStream(zipFilePath)
.pipe(unzipper.Extract({ path: destDir }))
.on('close', () => resolve(destDir))
.on('error', (err) => {
console.log('Error inside unzip', err);
reject(err);
});
});
};
module.exports = unZip;
index.test.js:
const unZip = require('./');
const fs = require('fs');
const sinon = require('sinon');
const unzipper = require('unzipper');
describe('69616649', () => {
afterEach(() => {
sinon.restore();
});
it('should pass', async () => {
sinon.stub(fs, 'mkdir').callsFake((path, callback) => {
callback();
});
const rs = {
pipe: sinon.stub().returnsThis(),
on: sinon.stub().callsFake(function (event, callback) {
if (event === 'close') {
callback();
}
}),
};
sinon.stub(fs, 'createReadStream').returns(rs);
sinon.stub(unzipper, 'Extract');
const actual = await unZip('fakeFileContent', 'fakeFileContent');
sinon.assert.match(actual, 'fakeFileContent');
sinon.assert.calledWithExactly(fs.mkdir, 'fakeFileContent', sinon.match.func);
sinon.assert.calledWithExactly(fs.createReadStream, 'fakeFileContent');
sinon.assert.calledWithExactly(unzipper.Extract, { path: 'fakeFileContent' });
sinon.assert.calledOnce(rs.pipe);
sinon.assert.calledWithExactly(rs.on, 'close', sinon.match.func);
});
});
測驗結果:
69616649
? should pass
1 passing (7ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 81.82 | 100 | 75 | 81.82 |
index.js | 81.82 | 100 | 75 | 81.82 | 13-14
----------|---------|----------|---------|---------|-------------------
uj5u.com熱心網友回復:
我是 ava 的新手,所以可能是錯誤的
proxyquire('../../../src/helpers/file/unZip')// actual function file
ava('69616649', () => {
ava.afterEach(() => {
sinon.restore();
});
ava.serial('should pass', async () => {
sinon.stub(fs, 'mkdir').callsFake((path, callback) => {
callback();
});
const rs = {
pipe: sinon.stub().returnsThis(),
on: sinon.stub().callsFake(function (event, callback) {
if (event === 'close') {
callback();
}
}),
};
sinon.stub(fs, 'createReadStream').returns(rs);
sinon.stub(unzipper, 'Extract');
const actual = await unZip('fakeFileContent', 'fakeFileContent');
sinon.assert.match(actual, 'fakeFileContent');
sinon.assert.calledWithExactly(
fs.mkdir,
'fakeFileContent',
sinon.match.func
);
sinon.assert.calledWithExactly(fs.createReadStream, 'fakeFileContent');
sinon.assert.calledWithExactly(unzipper.Extract, {
path: 'fakeFileContent',
});
sinon.assert.calledOnce(rs.pipe);
sinon.assert.calledWithExactly(rs.on, 'close', sinon.match.func);
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/324857.html
標籤:javascript 节点.js 单元测试 诗浓 阿瓦
