Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 测试使用另一个使用$http的服务的angular服务_Angularjs_Http_Mocking_Jasmine_Promise - Fatal编程技术网

Angularjs 测试使用另一个使用$http的服务的angular服务

Angularjs 测试使用另一个使用$http的服务的angular服务,angularjs,http,mocking,jasmine,promise,Angularjs,Http,Mocking,Jasmine,Promise,我已经写了两个角度服务 其中一个叫做“searchAPI”,它基本上接受用户输入,形成弹性搜索查询,然后通过一个$http.get调用启动它们 //searchAPI service = { executeSearch: function(input, resultsPerPage, pageNumber){ request = // some well tested logic that I know works to create a query string

我已经写了两个角度服务

其中一个叫做“searchAPI”,它基本上接受用户输入,形成弹性搜索查询,然后通过一个$http.get调用启动它们

//searchAPI

service = {
    executeSearch: function(input, resultsPerPage, pageNumber){
        request = // some well tested logic that I know works to create a query string
        return $http.get(request);
    }
}
还有一个叫做typeAhead,它使用我的searchAPI获取typeAhead结果列表

//typeAhead

service = {

    typeAheadContent: [],

    buildTypeAheadContent: function(input){
        return searchAPI.executeSearch(input, 10, 1).then(function(res){
            for(var i = 0; i < res.length; i++){
                service.typeAheadContent.push(res[i]);
            }
        });
    },  

    getTypeAheadResults: function(input){
        return service.buildTypeAheadContent(input).then(function(){
            return service.typeAheadContent;
        });     
    }
};
我在jasmine测试中尝试过这样做,但通过这种方式模拟它,我并没有调用承诺,只是设置了一个返回值

有人能帮我开始我的设置和嘲弄一些承诺吗

////编辑////

这是我的jasmine测试中每个函数之前的测试

var searchAPI, typeAhead;
beforeEach(inject($rootScope, $injector, $q)
{
    typeAhead = $injector.get('typeAhead');
    searchAPI = $injector.get('searchAPI');
    searchAPI.executeSearch = function(input, resultsPerPage, pageNumber){

        // this is being alerted just fine
        alert("Inside mock");
        return $q.when([
            'item1', 'item2', 'item3'
        ]);
    }
    $rootScope.$digest();
}));

it('should construct typeahead stuff', function(){

    searchAPI.executeSearch("hello", 10, 1).then(function(res){

        //this is not being alerted
        alert(res);
    });
    typeAhead.buildTypeAheadContent("test");
});

所以我包括了一些帮助调试的东西。警告“Inside Mock”的代码行确实收到了警告,因此我知道我分配给executeSearch的模拟内容设置正确。但是,.then块中的代码没有被警告,所以我的承诺不能被解决或者其他什么 至于您的问题-我可能会模拟它以反映原始API-用静态值模拟承诺-您可以使用
$q.when

searchAPI.executeSearch = function(){
    return $q.when([
        'item1',
        'item2',
        'item3'
    ]);
};

$q。当
将外部(非角度)承诺或简单值转换为角度承诺时-在本例中为您的数组。

感谢您的响应。午饭后我会试试这个,看看它是否符合我的需要,然后你就可以接受了:)不幸的是,这还不是我想要的。。在我的测试中,我尝试了searchAPI.executeSearch()。然后(函数(res){alert(res);});但未收到任何警报。请尝试在它之后添加一个
$rootScope.digest()
,假设您没有使用1.3。我不知道我是否正在使用,但我在设置searchAPI.executeSearch()后尝试添加一个$rootScope.digest(),并收到错误消息“$rootScope.digest不是函数”ok$rootScope。$digest修复了此问题,但是,当我尝试执行“searchAPI.executeSearch().then(function(res){alert(res);}”)时,仍然没有看到任何警报
searchAPI.executeSearch = function(){
    return $q.when([
        'item1',
        'item2',
        'item3'
    ]);
};