使用 Nock 进行模拟,仅模拟同一主机的特定路由
Mocking with Nock, mock only a specific route with the same host
我正在使用 Nock ( https://github.com/node-nock/nock ) 来模拟由我需要在我的应用程序中测试的端点调用的底层服务。在端点的实现中,我多次调用这个底层服务:
我想知道是否可以只模拟其中一个。让我们说这个:
http://samehost/specificrsc2/
目前我无法做到这一点。因为我收到这样的错误:
'FetchError: request to http://samehost/specificrsc1/ failed, reason:
Nock: No match for request {\n "method": "GET",\n "url":
"http://samehost/specificrsc1/",
这就是我嘲笑底层服务的方式:
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});
请注意,这已在 documentation 中列出。
我正在使用 Nock ( https://github.com/node-nock/nock ) 来模拟由我需要在我的应用程序中测试的端点调用的底层服务。在端点的实现中,我多次调用这个底层服务:
我想知道是否可以只模拟其中一个。让我们说这个: http://samehost/specificrsc2/
目前我无法做到这一点。因为我收到这样的错误:
'FetchError: request to http://samehost/specificrsc1/ failed, reason:
Nock: No match for request {\n "method": "GET",\n "url":
"http://samehost/specificrsc1/",
这就是我嘲笑底层服务的方式:
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});
请注意,这已在 documentation 中列出。