【发布时间】:2019-09-11 05:43:27
【问题描述】:
我在一个名为 utils.js 的文件中有一个包含一堆 util 函数的 javascript 文件
export const processListOfItems = (input): [] => {
let listOfItems = [];
for (var index = 0; index < rawPayload.length; ++index) {
listOfItems.push(someFunction(item));
}
return listOfItems;
};
someFunction 也在 utils.js 中定义。
对于测试,我想存根“someFunction”,但在弄清楚如何做时遇到了麻烦。看起来 sinon.spy() 可能是我想要的方法,但它看起来需要一个对象,我没有对象,因为它只是一个 utils 文件。
我的理想测试应该是这样的
describe('someFunction fails on an item', () => {
it('returns the array with the rest of the items', () => {
const items = ['hi', 'hello'];
// I want to make it such that, when we go into the getListOfItems code, we return 42 whenever we call someFunction, rather than going into the logic itself.
const someFunctionStub = sinon.stub(someFunction).returns(42);
expect(getListOfItems(items)).toEqual([42, 42]);
});
});
【问题讨论】:
标签: javascript mocking sinon stubbing