Mocking with Nock, mock only a specific route with the same host

安稳与你 提交于 2019-12-12 19:52:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!