javascript - 在 Jestjs 中替代 spyOn().and.callfake

标签 javascript jestjs

我以前在 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/

相关文章:

javascript - 无法在 selenium 3.0-beta3 中使用 geckodriver 0.10 使用 Javascript 和 selenium 自己的示例启动 Firefox

javascript - 使用Jquery拖放,从拖放的div中获取值

javascript - 如何在 lodash 版本 4 中获得 _.pick 功能

javascript - 从外部文件加载 cucumber /小 cucumber 中的数据

reactjs - Jest + enzyme : TypeError: Cannot read property 'htmlparser2' of undefined

javascript - 如何告诉用户使用浏览器检测在 Chrome 中打开页面

javascript - 如何在 javascript 中实现类似同步的行为?

vue.js - 使用 Jest/VueJS 按钮单击测试失败

reactjs - 如何在 Jest 中测试模拟函数的返回值

javascript - 我如何使用 Jest 来监视方法调用?