我以前在 jasmine 中有 spyOn().and.callFake
,它对我的测试有很大帮助,现在我正在使用 Jest,我在文档中发现 jest.spyOn()
存在但没有 callFake
。
我的问题:如何监视方法并使用 Jest 和 expect
调用 Fake?
最佳答案
jest.spyOn official documentation给出了很好的答案:
Note: By default, jest.spyOn also calls the spied method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use jest.spyOn(object, methodName).mockImplementation(() => customImplementation) or object[methodName] = jest.fn(() => customImplementation);
因此,在您的情况下,只需将伪造的方法传递给 customImplementation
。
关于javascript - 在 Jestjs 中替代 spyOn().and.callfake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879536/