Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用sinon模拟非类成员函数_Javascript_Testing_Mocha.js_Sinon - Fatal编程技术网

Javascript 如何使用sinon模拟非类成员函数

Javascript 如何使用sinon模拟非类成员函数,javascript,testing,mocha.js,sinon,Javascript,Testing,Mocha.js,Sinon,我需要将mock添加到method2函数中。但我犯了一个错误 “TypeError:试图将未定义的属性method2包装为函数” 您忘记导出异步方法2 // my-module.es6 export default class ServiceClass { async method1() { } } export async function method2() {} // test.js import { method2 } from 'my-module'; cons

我需要将mock添加到method2函数中。但我犯了一个错误

“TypeError:试图将未定义的属性method2包装为函数”


您忘记导出异步方法2

// my-module.es6

export default class ServiceClass {

  async method1() {

  }

}

export async function method2() {}

// test.js

import { method2 } from 'my-module';

const spy = sinon.spy(method2);
然而,不清楚你是否打算在课堂上使用method2?如果是这样的话,你会做与方法1相同的事情,然后做类似的事情

// test.js

import ServiceClass from 'my-module';

const serviceClass = new ServiceClass();

const spy = sinon.spy(serviceClass, 'method2');

您忘记导出异步方法2

// my-module.es6

export default class ServiceClass {

  async method1() {

  }

}

export async function method2() {}

// test.js

import { method2 } from 'my-module';

const spy = sinon.spy(method2);
然而,不清楚你是否打算在课堂上使用method2?如果是这样的话,你会做与方法1相同的事情,然后做类似的事情

// test.js

import ServiceClass from 'my-module';

const serviceClass = new ServiceClass();

const spy = sinon.spy(serviceClass, 'method2');

在方法2中,我调用了另一种方法3。我还加了一个mock。不幸的是,导出method2没有得到预期的输出

async function method2{
 method3();
}

method3(){
 //wrote mock here and it worked.
}

在方法2中,我调用了另一种方法3。我还加了一个mock。不幸的是,导出method2没有得到预期的输出

async function method2{
 method3();
}

method3(){
 //wrote mock here and it worked.
}

您是否尝试导出该函数?到目前为止,您只导出ServiceClass,因此无法从外部访问method2。您是否尝试导出该函数?到目前为止,您只导出ServiceClass,因此无法从Outside访问method2。即使您导出该方法,Non stub也不接受。但我在method2中使用了其他一些库方法。我模仿了那个库,得到了预期的输出。感谢您抽出时间。即使您导出了该方法,sinon stub也不接受。但我在method2中使用了其他一些库方法。我模仿了那个库,得到了预期的输出。谢谢你抽出时间。