2

我正在尝试根据angular docs为 e2e 测试目的设置和模拟 $httpBackEnd 。当我请求模板时,我想通过所有请求。从文档中,它说url参数可以是三种类型string function(string)或其RegExp描述:

HTTP url 或接收 url 并在 url 与当前定义匹配时返回 true 的函数。

我正在尝试做的事情

我希望所有direct/views/....GETpub/views/....请求PassThrough()

我试过的

var devApp = angular.module('app-dev', ['app', 'ngMockE2E']);

devApp.run(function($httpBackend){

    var isTemplateUrl = function(url){
        if(url === '/direct/views/_shell/shell' || url === '/direct/views/_shell/shell') {
            return true;
        }
        return false;
    };
//    Manually setting the url works
//    $httpBackend.whenGET('/direct/views/_shell/shell').passThrough();
//    $httpBackend.whenGET('/direct/views/home/home').passThrough();

//    Does not work
    $httpBackend.whenGET(isTemplateUrl).passThrough();
});

放置字符串有效,尝试使用该函数无效。

RegExp 可能是最快的方法,但我对此一无所知。如果您想给我一个有效的 RegExp,我将永远感激不尽。

我知道我的isTemplateUrl函数并不完全符合我的要求,但除此之外,它应该仍然适用于这两个 url。

4

3 回答 3

2

实现您想要做的事情的正则表达式是:

/(direct|pub)\/views\/.*$/
于 2014-06-11T15:40:22.343 回答
1

我在回答How to mock get(id) requests中看到使用函数作为方法的 url 匹配 *when**of httpbackend 从版本 1.3.0 开始有效。如果你看到其他版本的文档你会发现你不能使用这个功能。

您可以使用“test”方法将对象作为参数传递,而不是使用此函数。在您的情况下,这将是:

$httpBackend.whenGET({ test: isTemplateUrl}).passThrough();
于 2014-09-08T06:48:34.613 回答
1

如果您没有太多的网址槽,我建议您手动设置网址,因为使用正则表达式您允许任何类似

direct/views/*
pub/views/*

试试这个,看看它是否能解决你的问题。

var devApp = angular.module('app-dev', ['app', 'ngMockE2E']);

devApp.run(function($httpBackend){

    $httpBackend.whenGET(/^(direct|pub)\/views\/.*$/).passThrough();
});
于 2014-06-11T15:46:10.047 回答