【发布时间】:2022-01-14 06:28:45
【问题描述】:
我正在使用 Express 构建一个 back-for-front 应用程序。 它通过路由从前面专门调用,然后调用外部 API 来返回结果。这是逻辑的示例代码:
dashboard.route.ts
const router = Router();
const dashboardController = new DashboardController();
router.get("/distantCall", dashboardController.getDistantCall);
dashboard.controller.ts
import { Request, Response, NextFunction } from "express";
import DashboardService from "../services/dashboard.service";
export class DashboardController {
async getDistantCall(req: Request, res: Response, next: NextFunction) {
DashboardService.getDistantCalls()
.then((result: any) => {
res.status(200).send(result);
}).catch((error: any) => {
next(error);
});
}
}
dashboard.service.ts
import { DashboardApi } from './dashboard.api';
class DashboardService {
public async getDistantCall() {
return new Promise((resolve, reject) => {
new DashboardApi().getDistantCall()
.then((response: any) => {
resolve({
distantResponse: response.body
});
})
.catch((error) => {
reject(error);
});
});
}
DashboardAPI 类进行外部 http 调用并返回一个承诺。对于这个示例,它返回一个简单的文本“distantSuccess”
对于我的测试,我可以很容易地编写集成测试
dashboard.routes.spec.ts
import chai from "chai";
import chaiHttp from "chai-http";
import { expect } from "chai";
chai.use(chaiHttp);
import createServer from "../../src/server";
const app = createServer();
describe("dashboard routes", function() {
it('nominal distant call', async () => {
const res = await chai.request(app).get("/dashboard/distantCall");
expect(res.status).to.eq(200);
expect(res.body).to.be.a('object');
expect(res.body).to.have.property('distantResponse');
expect(res.body.distantResponse).to.eq('distantSuccess');
});
});
我的问题是构建 unit 测试。据我了解,我应该只测试控制器或服务,并使用模拟和存根来模拟范围之外的元素。这是我做的两个测试:
dashboard.controller.spec.ts
import { Request, Response, NextFunction } from "express";
import chai from "chai";
import chaiHttp from "chai-http";
import { expect } from "chai";
import sinon from "sinon";
chai.use(chaiHttp);
import createServer from "../../src/server";
const app = createServer();
import { DashboardController } from "../../src/controllers/dashboard.controller";
const dashboardController = new DashboardController();
import DashboardService from "../../src/services/dashboard.service";
describe("dashboard routes with fake objects", function () {
it("distant call by controller", async () => {
const mockRequest: any = {
headers: {},
body: {},
};
const mockResponse: any = {
body: { distantResponse: "About..." },
text: "test",
status: 200,
};
const mockNext: NextFunction = () => {};
await dashboardController.getDistantCallSucces(mockRequest, mockResponse, mockNext);
expect(mockResponse.status).to.eq(200);
expect(mockResponse.body).to.be.a("object");
expect(mockResponse.body).to.have.property("distantResponse");
expect(mockResponse.body.distantResponse).to.eq("About...");
});
});
describe("dashboard routes with stubs", function () {
before(() => {
sinon
.stub(DashboardService, "getDistantCall")
.yields({ distantResponse: "distantSuccess" });
});
it("distant call by controller", async () => {
const mockRequest: any = {};
const mockResponse: any = {};
const mockNext: NextFunction = () => {};
const res = await dashboardController.getDistantCall(mockRequest, mockResponse, mockNext);
console.log(res);
});
});
对于第一次测试,我显然不了解它的用途。我正在测试我刚刚创建的对象,甚至不知道是否调用了该服务。 我觉得我应该做一些更像第二次测试的事情,但我得到了这个错误: TypeError: getDistantCall 预期会产生,但没有通过回调。
【问题讨论】:
标签: node.js express unit-testing chai sinon