问题
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/
- ...
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:
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.
来源:https://stackoverflow.com/questions/47798860/mocking-with-nock-mock-only-a-specific-route-with-the-same-host