根据文档,Sinon可以使用regexp模式来匹配URL:
Server.respondWith(方法,urlRegExp,响应);
我想匹配所有以foo=1结尾的URL。以下是我的尝试:
this.server.respondWith('GET', '/foo=1$/', [200,
{ 'Content-Type': 'application/json' }, '{ "foo": 1 }']);然而,它似乎不起作用。我的regexp可能是错的,但我需要你帮我调整一下。
http://jsfiddle.net/s38qw3ns/1/
发布于 2015-08-31 06:56:48
正则表达式周围有单引号,所以sinon将其视为字符串(它是:)。
去掉单引号,它将是一个正则表达式,并如您所期望的那样工作。
this.server.respondWith('GET', /foo=1$/, [200, { 'Content-Type': 'application/json' }, '{ "foo": 1 }']);
有关参考,请参阅MDN上的创建正则表达式。
https://stackoverflow.com/questions/30170717
复制相似问题