简体   繁体   English

茉莉花:带有可变网址的存根ajax请求

[英]Jasmine: Stub ajax request with a variable URL

Using Jasmine (with jasmine-ajax ), I need to stub an ajax request wherein the URL has a dynamically generated parameter. 使用Jasmine(使用jasmine-ajax ),我需要对ajax请求进行存根处理,其中URL具有动态生成的参数。 Example: 例:

jasmine.Ajax
  .stubRequest('search?generatedParameter=...')
  .andReturn({responseText: '...'});

However, Jasmine doesn't seem to allow wildcards in the URL. 但是,Jasmine似乎不允许在URL中使用通配符。

Question: Is there any way using Jasmine to stub an ajax request for a variable URL? 问题:有什么方法可以使用Jasmine将ajax请求存入变量URL?

Jasmine-ajax allows a regex to be passed for the url: Jasmine-ajax允许为URL传递正则表达式:

jasmine.Ajax.stubRequest(/search\?generatedParameter=.+/);

See https://github.com/jasmine/jasmine-ajax (under the Complex Requests heading) for more info. 有关更多信息,请参见https://github.com/jasmine/jasmine-ajax (在“复杂请求”标题下)。

我能够使用response方法触发对最新ajax请求的响应:

  jasmine.Ajax.requests.mostRecent().response({responseText: '...', status: 200});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM