【发布时间】:2019-07-22 11:50:13
【问题描述】:
我是 jest 和 react 的新手,我需要为这个函数编写一个单元测试,我真的试图理解文档,但我很难理解如何准确地模拟这个函数?
function desc(a, b, orderBy) {
if (b[orderBy] < a[orderBy]) {
return -1;
}
if (b[orderBy] > a[orderBy]) {
return 1;
}
return 0;
}
下面也是我的笑话测试,它通过但没有断言或行覆盖
describe('Contract Table Ordering', () => {
it('orders by desc', () => {
contract.desc.mockImplementation(async () => { return (a, b, orderBy)
=> (a[orderBy] > b[orderBy]) - (a[orderBy] < b[orderBy]); });
});
});
【问题讨论】:
-
为什么要模拟它?如果你嘲笑它,那么你就没有测试它。似乎您更想用数据调用它,然后检查数据是否符合您的期望。
标签: javascript node.js reactjs jestjs