如何将测试用例写入类。我有一个类里面有两种方法。我通过创建新对象在我的 route 访问此类。现在有了测试用例,我必须模拟实际的类方法调用。我正在使用 sinon 和 mocha。它仍然在调用原来的类。
如何将测试用例写入我的路由器
我的类1.js
export default class Detector {
constructor() {}
setDetectorStrategy(Detector) {
this.detector = Detector;
}
getcontent = async(payload) => {
console.log('kkkkkkkkkkkkkkkkkkkkkkkkkkkk');
const res = await this.getDetails(payload);
return res;
}
}
在 myRoute.js 中
import Detector from '../services/detector';
router.post('/', async(req, res) => {
let detector = new Detector();
detector.setDetectorStrategy(new my());
await detector.getcontent (payload);
res.json({});
});
最佳答案
请查看下面 GitHub 中的示例,看看它是否有帮助。
spy ,用sinonjs创建,监视服务器变量(supertest)封装并运行server.js,并用sinon的assert方法断言,以确定对/about路由的请求次数定义在服务器.js 文件。您可能需要修改代码以指向您的路线。
https://github.com/ravichandranjv/SpyingARoute/blob/master/test/usertest.js
关于javascript - 如何使用 mocha sinon 模拟类异步/等待函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371011/