单元测试控制器使用Jest,NodeJS



我想检查某些路由调用正确的控制器的情况,使用Jest特定(模拟或间谍)。

它特定于单元测试。有人可以帮助我如何使用开玩笑来检查它。我不需要验证 期望(状态代码或 res 对象)我需要检查是否已调用控制器。 谢谢!

例如:

// todoController.js
function todoController (req, res) {
res.send('Hello i am todo controller')
} 
// index.spec.js
const express = require('express');
const request = require('request-promise');
const todoController = require('./todoController');
jest.mock('./todoController');
const app = express();
app.get('/todo', todoController)
test('If certain routes are calling the correct controller , controller should to have been called times one.', async() => {
await request({url: 'http://127.0.0.1/todo'})
expect(todoController).toHaveBeenCalledTimes(1);
})

实际上,如果您搜索,那里有很多参考资料。

在下文中,我分享了我知道的几种方法。

使用模拟请求/响应测试Express应用程序的一大概念飞跃是了解如何模拟链式

应用程序API 例如。res.status(200).json({ foo: 'bar' }).

首先,您可以制作某种拦截器,这是通过从其每个方法返回 res 实例来实现的:

// util/interceptor.js
module.exports = {
mockRequest: () => {
const req = {}
req.body = jest.fn().mockReturnValue(req)
req.params = jest.fn().mockReturnValue(req)
return req
},
mockResponse: () => {
const res = {}
res.send = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
return res
},
// mockNext: () => jest.fn()
}

Express 用户土地 API 基于中间件。一个中间件,它将请求(通常称为 req)、响应(通常称为 res)和下一个(调用下一个中间件)作为参数。

然后你有这样的控制器:

// todoController.js
function todoController (req, res) {
if (!req.params.id) {
return res.status(404).json({ message: 'Not Found' });
}
res.send('Hello i am todo controller')
}

它们通过在 Express 应用程序(应用程序)实例(在 app.js 中)上"挂载"来使用:

// app.js
const express = require('express');
const app = express();
const todoController = require('./todoController');
app.get('/todo', todoController);

使用我们之前定义的 mockRequest 和 mockResponse,我们将确定res.send()是使用正确的有效负载({ data })调用的。

所以在你的测试文件上:

// todo.spec.js
const { mockRequest, mockResponse } = require('util/interceptor')
const controller = require('todoController.js')
describe("Check method 'todoController' ", () => {
test('should 200 and return correct value', async () => {
let req = mockRequest();
req.params.id = 1;
const res = mockResponse();
await controller.todoController(req, res);
expect(res.send).toHaveBeenCalledTimes(1)
expect(res.send.mock.calls.length).toBe(1);
expect(res.send).toHaveBeenCalledWith('Hello i am todo controller');
});
test('should 404 and return correct value', async () => {
let req = mockRequest();
req.params.id = null;
const res = mockResponse();
await controller.todoController(req, res);
expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ message: 'Not Found' });
});
});

这只是测试快速处理程序和中间件的一种方法。另一种方法是启动 Express 服务器。

相关内容

最新更新