nock.js – 如何匹配URL中的任何参数组合

我正在尝试模拟对此APIurl的响应

http://api.myapihost.com/images?foo=bar&spam=egg 

url参数组合可能会有所不同。 我试图拦截这个请求,并用一个空的对象作出回应。

 nock('http://api.myapihost.com') .persist() .get('/images', '*') .reply(200, {}); 

当我的testing用例运行时,我得到这个错误信息:

 Uncaught Error: Nock: No match for HTTP request GET /images?height=2500 

我如何configurationnock来匹配URL参数的任意组合?

您应该使用path过滤为了匹配URL参数。

 var scope = nock('http://api.myapihost.com') .filteringPath(function(path) { return '/images'; }) .get('/images') .reply(200, {}); 

你可以在这里查看文档