【发布时间】:2020-08-13 18:30:37
【问题描述】:
请考虑以下情况,但我无法解决。在路由器(“/”)下调用一个类。我想测试路由器(“/”)调用并模拟 myFunc 结果。
class A {
myFunc = async () => {
await ...
return result
}
}
Controller file - C:
router.get("/", async (req, res) => {
var a = new A();
a.myFunc()
.then(result => {
res.json({"message": result});
}).catch(e => {
return []
})
});
C.test.js:
const request = require("supertest");
const app = require("../C");
jest.mock('A');
test("Test route /", async done => {
const myFuncMock = jest.fn().mockImplementation(() => [...]);
A.prototype.myFunc = myFuncMock;
await request(app)
.get("/")
.then(res => {
expect(res.status).toBe(200); // Not asserted
});
done()
});
我无法在路由器下模拟函数结果。
【问题讨论】:
标签: node.js mocking jestjs integration-testing supertest