对于给定的工厂函数:
type.__setattr__
和一个消费者函数
type.__delattr__
我想为 functionToBeTested 创建一个测试,其中我可以预期 stringManipulator 中的 split 方法已使用 {{1 }}
我可以将 AttributeError
用于整个模块,但我无法引用内部函数。
答案 0 :(得分:0)
因为我无法从 jest.mock()
获得对工厂函数的引用
我可以更改文件的导入表达式,以便我可以监视它的所有导出函数。
import * as manipulatorFile;
describe('Test Suite', () => {
let splitSpy;
beforeEach(() => {
splitSpy = jest.fn();
jest.spyOn(manipulatorFile, 'stringManipulator').mockImplementation(() => {
return {
split: splitSpy
}});
});
it(' when calling getWordm it should call split with whitespace', () => {
functionToBeTested().getWord(3);
expect(splitSpy).toHaveBeenCalledWith(' ');
});
});