使用 ts-mockito 和 Typescript 模拟快速请求

Mocking Express Request with ts-mockito and Typescirpt

是否可以在 typescript 中模拟来自 Express using ts-mockito 的 class 请求?

我尝试了以下方法

import { Request, Response } from "express";  

const request = mock(Request);
const req: Request = instance(request);

但在 req 上收到错误消息:Type 'Request' is missing the following properties from type 'Request<ParamsDictionary>': get, header, accepts, acceptsCharsets, and 73 more.

请求是一个接口,所以你应该使用这个语法:

import { Request, Response } from "express";
import { mock, instance } from "ts-mockito";

const mockReq = mock<Request>();
const req = instance(mockReq);

参考ts-mockito documentation了解更多。