我有2个功能,
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);
});
})
我需要为此测试创建存根吗?我是单元测试的新手。测试正确通过。
这样,您将无法对其进行完整的测试。更好的方法是使用nock
模拟http调用并端对端测试功能。您可以在此处查看示例
https://www.npmjs.com/package/nock
您正在测试的方式,它实际上并没有进行任何测试,因为它完全用您正在使用的spy
代替了该功能。