如何在开玩笑中测试 returns 匿名函数的函数?
How to test a function which returns anonymous function in jest?
我正在测试一个 return 是匿名函数的函数。我面临的问题是测试没有涵盖函数的 return 部分,即 return 匿名函数。
代码-
tweenPie = (b) => {
const { arc } = this;
b.innerRadius = 0;
let i = d3.interpolate({ startAngle: 0, endAngle: 0 }, b);
return (t) => arc(i(t));
};
测试
test("spy on tweenPie function", () => {
const wrapper = setup();
const spy = jest.spyOn(wrapper.instance(), "tweenPie");
wrapper.instance().tweenPie({});
expect(spy).toHaveBeenCalled();
});
我的测试覆盖了行 return (t) => arc(i(t));在函数中。怎么做?
tweenPie
实际上不是 return 一个值,而是 return 另一个 function
。因此,当您调用 wrapper.instance().tweenPie({});
时,您得到的是函数而不是值。
为了测试该功能,只需再次调用,如下所示。
// Pass some value that corresponds to t in (t) => arc(i(t));
wrapper.instance().tweenPie({})(t);
我正在测试一个 return 是匿名函数的函数。我面临的问题是测试没有涵盖函数的 return 部分,即 return 匿名函数。 代码-
tweenPie = (b) => {
const { arc } = this;
b.innerRadius = 0;
let i = d3.interpolate({ startAngle: 0, endAngle: 0 }, b);
return (t) => arc(i(t));
};
测试
test("spy on tweenPie function", () => {
const wrapper = setup();
const spy = jest.spyOn(wrapper.instance(), "tweenPie");
wrapper.instance().tweenPie({});
expect(spy).toHaveBeenCalled();
});
我的测试覆盖了行 return (t) => arc(i(t));在函数中。怎么做?
tweenPie
实际上不是 return 一个值,而是 return 另一个 function
。因此,当您调用 wrapper.instance().tweenPie({});
时,您得到的是函数而不是值。
为了测试该功能,只需再次调用,如下所示。
// Pass some value that corresponds to t in (t) => arc(i(t));
wrapper.instance().tweenPie({})(t);