gpt4 book ai didi

javascript - 单元测试 Controller 使用 Jest、NodeJS

转载 作者:行者123 更新时间:2023-11-30 19:40:24 24 4
gpt4 key购买 nike

我想检查某些路由调用正确 Controller 使用 Jest 特定 (模拟或 spy ) 的情况。

它是特定于单元测试的案例。有人可以帮助我如何使用 Jest 来检查它。我不需要验证expect (状态代码或 res 对象)我需要检查 controller 是否已被调用。谢谢!

例如:

// 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)和 next(调用 next 中间件)作为参数的中间件。

然后你有这样的 Controller :

// 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) 实例(在 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 处理程序和中间件的一种方法。另一种方法是启动 Express 服务器。

关于javascript - 单元测试 Controller 使用 Jest、NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55463886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com