与Nock进行模拟,仅模拟具有相同主机的特定路由 [英] Mocking with Nock, mock only a specific route with the same host

查看:232
本文介绍了与Nock进行模拟,仅模拟具有相同主机的特定路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nock( https://github.com/node-nock/nock)来模拟由端点调用的底层服务,我需要在应用程序中对其进行测试.在端点的实现中,我多次将这种底层服务称为:

I am using Nock ( https://github.com/node-nock/nock ) to mock an underly service called by an endpoint that I need to test in my application. In the implementation of the endpoint, I am calling this underly service multiple time like that :

  • http://samehost/specificrsc1/
  • http://samehost/specificrsc2/
  • http://samehost/specificrsc3/
  • ...

我想知道是否可能只嘲笑其中的一个.假设这个: http://samehost/specificrsc2/

目前,我无法实现这一目标.因为我收到这样的错误:

Currently i am not able to achieve that. Because I get an error like this :

'FetchError: request to http://samehost/specificrsc1/ failed, reason: 
 Nock: No match for request {\n  "method": "GET",\n  "url": 
"http://samehost/specificrsc1/",

这就是我嘲笑底层服务的方式:

Thats how i am mocking the underly service :

const mockUnderlyCall = nock('http://samehost');
mockUnderlyCall.get('/samehost/specificrsc1').reply(200, mockData)

我也尝试:

const mockUnderlyCall = nock('http://samehost/samehost/specificrsc1');
mockUnderlyCall.get('').reply(200, mockData)

谢谢!

推荐答案

我想知道是否可能只模拟其中一个

I want to know if it's possible to only mock ONE of those

是的,请使用allowUnmocked选项.

const mockUnderlyCall = nock('http://samehost', {allowUnmocked: true});

请注意,这在中列出文档(针对nock).

Note, this is listed in the documentation for nock.

这篇关于与Nock进行模拟,仅模拟具有相同主机的特定路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆