Javascript 另一个文件中的spyOn本地函数

Javascript 另一个文件中的spyOn本地函数,javascript,unit-testing,typescript,jasmine,Javascript,Unit Testing,Typescript,Jasmine,如何监视已导出但也在以下函数中调用的函数: 文件1: export function myFunction(){ // do stuff return myOtherFunction() } export function myOtherFunction(){ // do stuff } 文件2: import * as helpers from './helpers' beforeEach(()=>{ spyOn(helpers, 'myOtherFunction')

如何监视已导出但也在以下函数中调用的函数:

文件1:

export function myFunction(){
  // do stuff
  return myOtherFunction()
}

export function myOtherFunction(){
  // do stuff
}
文件2:

import * as helpers from './helpers'
beforeEach(()=>{
  spyOn(helpers, 'myOtherFunction');
});
it('should call myOtherFunction', ()=>{
  helpers.myFunction();
  expect(helpers.myOtherFunction).toHaveBeenCalled()
});

问题是,
myOtherFunction
永远不会被调用,因为jasmine正在监视错误的对象
myFunction
将调用本地函数,jasmine测试将期望调用导出的变量,它们是不同的对象。

这个问题与我不久前遇到的问题类似。@JeffreyWesterkamp我同意。我认为这是一个重复,但其他人的解决方案只适用于我导出的类只是一个函数。这里的问题实际上是相同的。仅覆盖文件2中的
helpers.myOtherFunction
引用
myFunction
但是使用直接本地引用。这也是你的期望落空的原因。文件2中被覆盖的引用实际上从未被调用,只有实际的实现。本地引用永远不会是其他任何内容,除非从文件1中显式重写。