Sinon fakeserver url 正则表达式

Sinon fakeserver url regexp

根据 documentation,Sinon 伪造服务器可以使用正则表达式模式来匹配 URL:

server.respondWith(method, urlRegExp, response);

我想匹配所有以 foo=1 结尾的 URL。这是我的尝试:

this.server.respondWith('GET', '/foo=1$/', [200, 
{ 'Content-Type': 'application/json' }, '{ "foo": 1 }']);

不过,好像不行。我的正则表达式可能是错误的,但我需要你帮助调整它。

http://jsfiddle.net/s38qw3ns/1/

您的正则表达式用单引号括起来,所以 sinon 将其视为字符串(它是 :)。

去掉单引号,它将成为正则表达式并按您预期的方式工作。

this.server.respondWith('GET', /foo=1$/, [200, { 'Content-Type': 'application/json' }, '{ "foo": 1 }']);

参见 MDN 上的 Creating a regular expression 以供参考。