简体   繁体   中英

Mocking Express Request with ts-mockito and Typescirpt

Is it possible to mock the class Request from Express using ts-mockito in typescript?

I tried the following

import { Request, Response } from "express";  

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

but get an error on req stating: Type 'Request' is missing the following properties from type 'Request<ParamsDictionary>': get, header, accepts, acceptsCharsets, and 73 more.

Request is an interface, so you should use this syntax:

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

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

Refer to the ts-mockito documentation for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM