[英]Test case for node js with sinon and chai
I am trying to use sinon to do a unit test for one of my node functions.我正在尝试使用 sinon 对我的节点功能之一进行单元测试。
Below is the actual function.下面是实际功能。
import { replaceYears, getFinancialYears } from './utils/years/';
const replaceAssetFileName = () => {
const years = getFinancialYears(); // return array of years [2019, 2018, 2017]
for (let i = 0; i < years.length; i++) {
replaceYears(years[i], 'NEWNAME'); // replace years just replace 2019 to FY19, don't ask why a function is written for this.
}
return true;
};
I want to mock the function getFinancialYears
, so that just for test the function can return only one or two years instead of 100s of years.我想模拟函数getFinancialYears
,以便仅用于测试该函数只能返回一两年而不是 100 年。
I tried below test case with sinon and chai.我用 sinon 和 chai 尝试了下面的测试用例。 But still I see the function “getFinancialYears” giving out the actually list of years instead of fakes.但是我仍然看到函数“getFinancialYears”给出了实际的年份列表而不是假的。
it('We can replace file names', () => {
const stub = sinon.stub(util, 'getFinancialYears').callsFake(() => ['2019']);
expect(replaceAssetFileName()).to.be(true);
stub.restore();
}).timeout(20000);
Here is the solution to stub getFinancialYears
function:这是存根getFinancialYears
函数的解决方案:
index.ts
: index.ts
:
import { replaceYears, getFinancialYears } from './utils/years';
export const replaceAssetFileName = () => {
const years = getFinancialYears();
console.log(years);
for (let i = 0; i < years.length; i++) {
replaceYears(years[i], 'NEWNAME');
}
return true;
};
utils/years.ts
: utils/years.ts
:
export function getFinancialYears() {
return ['2019', '2018', '2017'];
}
export function replaceYears(year, replacer) {}
index.spec.ts
: index.spec.ts
:
import { replaceAssetFileName } from './';
import sinon from 'sinon';
import { expect } from 'chai';
import * as util from './utils/years';
describe('replaceAssetFileName', () => {
it('We can replace file names', () => {
const logSpy = sinon.spy(console, 'log');
const stub = sinon.stub(util, 'getFinancialYears').callsFake(() => ['2019']);
expect(replaceAssetFileName()).to.be.true;
expect(logSpy.calledWith(['2019'])).to.be.true;
stub.restore();
});
});
Unit test result with coverage report:带有覆盖率报告的单元测试结果:
replaceAssetFileName
[ '2019' ]
✓ We can replace file names
1 passing (10ms)
----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
All files | 95.65 | 100 | 83.33 | 95.24 | |
55573978 | 100 | 100 | 100 | 100 | |
index.spec.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
55573978/utils | 66.67 | 100 | 50 | 66.67 | |
years.ts | 66.67 | 100 | 50 | 66.67 | 2 |
----------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/55573978源代码: https : //github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/55573978
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.