【问题标题】:Create a mock Request object in Loopback 4在 Loopback 4 中创建一个模拟请求对象
【发布时间】:2021-12-03 18:52:32
【问题描述】:

我有一个 Loopback 4 控制器,我想对它进行单元测试。如何模拟请求参数?这是我的控制器的构造函数:

  constructor(@inject(RestBindings.Http.REQUEST) private req: Request,
              @service(HttpService) private httpService: HttpService,
              @service(LocalRequestService) private localRequestService: LocalRequestService) {}

我尝试用createStubInstance(Request) 模拟它,但这给了我这个错误:

Argument of type 'StubbedInstanceWithSinonAccessor<Request>' is not assignable to parameter of type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
  Type 'StubbedInstanceWithSinonAccessor<Request>' is missing the following properties from type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>': get, header, accepts, acceptsCharsets, and 68 more.ts(2345)

【问题讨论】:

    标签: unit-testing request mocking loopback4


    【解决方案1】:

    可以使用stubExpressContext。

    const context = stubExpressContext();
    const controller = new MyController(context.request);
    

    可以将选项传递给 stubExpressContext。

    More..

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多