如何使用 sinon 模拟非 class 成员函数
How to mock non class member function using sinon
我需要将 mock 添加到 method2 函数中。但我收到错误
"TypeError: Attempted to wrap undefined property method2 as function"
class ServiceClass {
async method1() {
}
}
async function method2() {}
module.exports = ServiceClass;
您忘记导出您的异步方法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);
但是,不清楚您是否希望方法 2 出现在您的 class 中?如果是这样的话,你会做和 method1 一样的事情,做这样的事情
// test.js
import ServiceClass from 'my-module';
const serviceClass = new ServiceClass();
const spy = sinon.spy(serviceClass, 'method2');
在方法 2 中我正在调用另一个方法 3。我为此添加了模拟。不幸的是,导出 method2 没有给出预期的输出。
async function method2{
method3();
}
method3(){
//wrote mock here and it worked.
}
我需要将 mock 添加到 method2 函数中。但我收到错误
"TypeError: Attempted to wrap undefined property method2 as function"
class ServiceClass {
async method1() {
}
}
async function method2() {}
module.exports = ServiceClass;
您忘记导出您的异步方法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);
但是,不清楚您是否希望方法 2 出现在您的 class 中?如果是这样的话,你会做和 method1 一样的事情,做这样的事情
// test.js
import ServiceClass from 'my-module';
const serviceClass = new ServiceClass();
const spy = sinon.spy(serviceClass, 'method2');
在方法 2 中我正在调用另一个方法 3。我为此添加了模拟。不幸的是,导出 method2 没有给出预期的输出。
async function method2{
method3();
}
method3(){
//wrote mock here and it worked.
}