【发布时间】:2019-01-20 14:16:37
【问题描述】:
我有一个打字稿接口cRequest,它作为类方法中的类型传递。
该接口还扩展了express 请求类型。在jest 或typemoq 中模拟这个的正确方法是什么?
import { Request } from 'express';
import { User } from '.';
export interface cRequest extends Request {
context: {
ID?: string;
user?: string;
nonUser?: string;
};
user?: User;
}
export default cRequest;
在类方法中使用如下
import { Response } from 'express';
public getData = async (req: cRequest, res: Response) => {}
如果我尝试如下测试它会失败
const expRespMock: TypeMoq.IMock<Response> = TypeMoq.Mock.ofType<Response>();
const cReqMock: TypeMoq.IMock<cRequest> = TypeMoq.Mock.ofType<cRequest>();
await classObj.getData(cReqMock, expRespMock);
带有以下信息
Argument of type 'IMock<cRequest>' is not assignable to parameter of type 'cRequest'.
Property 'context' is missing in type 'IMock<cRequest>'.
在测试中将此接口模拟注入方法的正确方法是什么?
【问题讨论】:
标签: javascript typescript jestjs typemoq