Javascript 期望中间件functin调用下一个函数或发送http 400代码
我想进入单元测试,并想测试我的节点API。我将Express与Typescript结合使用,对于测试,我使用Jest。在调用控制器中间件之前,我调用路由输入验证中间件来检查参数和主体变量是否有效。否则我将发送一个400错误代码Javascript 期望中间件functin调用下一个函数或发送http 400代码,javascript,node.js,typescript,express,jestjs,Javascript,Node.js,Typescript,Express,Jestjs,我想进入单元测试,并想测试我的节点API。我将Express与Typescript结合使用,对于测试,我使用Jest。在调用控制器中间件之前,我调用路由输入验证中间件来检查参数和主体变量是否有效。否则我将发送一个400错误代码 this.router.route('/') .post(userValidations.validateCreateUser, userController.createUser); 此验证将创建一个要验证的架构 import { Request, Respo
this.router.route('/')
.post(userValidations.validateCreateUser, userController.createUser);
此验证将创建一个要验证的架构
import { Request, Response, NextFunction } from 'express';
import joi, { SchemaLike } from '@hapi/joi';
import { validateRequest } from '../validator';
export function validateCreateUser (request: Request, response: Response, next: NextFunction) {
const validationSchema: SchemaLike = joi.object().keys({
body: joi.object().keys({
username: joi.string().required(),
password: joi.string().required(),
}).unknown(true)
}).unknown(true);
validateRequest(validationSchema, request, response, next);
}
并调用valdateRequest
方法。此方法根据validationSchema
验证request
对象,如果请求输入有效,则调用下一个函数。否则,它将以400错误代码响应并发回错误详细信息
import { Request, Response, NextFunction } from 'express';
import joi, { SchemaLike, ValidationResult, ValidationError } from '@hapi/joi';
export function validateRequest(validationSchema: SchemaLike, request: Request, response: Response, next: NextFunction) {
const validationResult: ValidationResult<Request> = joi.validate(request, validationSchema, {
abortEarly: false
});
const validationError: ValidationError = validationResult.error;
if (validationError) {
response.status(400).json({
message: 'The route validation failed.',
details: validationError.details,
});
} else {
next();
}
}
我正在努力解决三个问题
- 如何创建
请求的实例
- 如何创建
响应的实例
- 我有什么期待<代码>预期(validateRequest(validationSchema、请求、响应、下一步))。到…
如果有人能解释如何正确地测试它,那就太棒了。也许我必须重新构造我的业务代码,但我试图编写可测试的…下面的代码是API级别的单元测试用例逻辑
import * as request from "supertest";
import * as app from "../src/app"; // Main file
import "reflect-metadata";
const faker = require("faker/locale/en_IND");
describe('The route input validator', () => {
it('Calls the API - POST /api/foo', () => {
const first_name = faker.name.firstName();
const last_name = faker.name.lastName();
const email = faker.internet.email();
request(app).post("/api/foo")
.set("accept", "application/json")
.set("content-type", "application/json")
.send({
"firstName": firstName,
"email": email,
"mobile_no": mobileNo
}).end(function (err, res) {
// Something like this - Assertion
expect(res.body.success).toBe(true);
}).expect(200);
});
});
上面的代码与我使用正确断言进行完整API级测试时使用的代码类似。应该能够让您开始。为什么不从Jest单元测试用例文件中进行实际的API调用(API级测试),而不是手动创建
请求
对象?如果这样对你有用,请告诉我。我将发布一个解决方案。当然,这会起作用,但我认为这不会测试单个单元,对吗=?因为API调用只会测试路由、验证器、控制器。。。在一次电话中,我想是的。它将测试完整的API。它对你有用吗?是的,这对我有用,但我想先测试单个函数。我不想测试整个问题,我想单独测试每个部分。我会调查并尝试更新答案。谢谢。我评论了问题评论部分
import * as request from "supertest";
import * as app from "../src/app"; // Main file
import "reflect-metadata";
const faker = require("faker/locale/en_IND");
describe('The route input validator', () => {
it('Calls the API - POST /api/foo', () => {
const first_name = faker.name.firstName();
const last_name = faker.name.lastName();
const email = faker.internet.email();
request(app).post("/api/foo")
.set("accept", "application/json")
.set("content-type", "application/json")
.send({
"firstName": firstName,
"email": email,
"mobile_no": mobileNo
}).end(function (err, res) {
// Something like this - Assertion
expect(res.body.success).toBe(true);
}).expect(200);
});
});