Mocking with Nock, mock only a specific route with

2019-07-16 09:20发布

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 :

I want to know if it's possible to only mock ONE of those. Let's say this one : 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)

I also try :

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

Thank you !

1条回答
在下西门庆
2楼-- · 2019-07-16 09:25

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

Yes, use the allowUnmocked option.

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

Note, this is listed in the documentation for nock.

查看更多
登录 后发表回答