【发布时间】:2020-02-24 15:04:53
【问题描述】:
我有两个功能,
const callAndParseHttp = async (url) => {
const response = await got(url);
return await parseXml(response.body);
};
const parseXml = async xmlData => {
try {
const json = parser.toJson(xmlData.body);
return JSON.parse(json);
} catch (err) {
return err;
}
};
我在 sinon 中写了一个单元测试,看起来像,
describe('/ handler', () => {
let spy;
before(() => {
spy = sinon.spy(unitParser, 'callAndParseHttp');
});
afterEach(() => {
spy.restore();
});
it('unit parser testing', async () => {
await unitParser.callAndParseHttp(
'http://www.mocky.io/v2/5e34242423'
);
expect(spy.callCount).to.equal(1);
});
})
我需要为此测试创建存根吗?我是单元测试的新手。测试正确通过。
【问题讨论】:
标签: javascript node.js mocha.js chai sinon