我有一个文件mainFile
,它使用方法helperMethod
,该方法是一种在文件helperFile
中返回Promise的方法。我如何对helperMethod
返回的内容进行存根处理?
这是我到目前为止所拥有的-
helperFile:
export function helperMethod() {return a Promise}
module.exports.helperMethod = helperMethod;
mainFile:
import helperMethod from helperFile;
methodInMainFile() {console.log(helperMethod);}
mainFileTest:
import methodInMainFile from mainFile;
import * as utils from helperFile;
sinon
.stub(utils, 'helperMethod')
.returns(Promise.resolve(madeUpResponse));
methodInMainFile();
上面的代码显示Promise { undefined }
。我如何打印Promise { madeUpResponse }
?我不认为helperMethod实际上是被调用的(因为它不应该被调用),因为我在那里记录了一些东西,但从未显示。
答案 0 :(得分:0)
madeUpResponse
未定义。
您必须定义madeUpResponse
import methodInMainFile from mainFile;
import * as utils from helperFile;
const madeUpResponse = 'a string';
sinon
.stub(utils, 'helperMethod')
.returns(Promise.resolve(madeUpResponse));
methodInMainFile();
答案 1 :(得分:0)
本质上,您是通过导入将helperMethod分配给其自己的变量,因此,将方法存入exports对象不会修改mainFile中的原始函数。下面的代码应该可以工作:
mainFile:
import * as utils from './helperFile'
methodInMainFile() {console.log(utils.helperMethod)}