Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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中为控制器编写Jasmine测试用例_Angularjs_Jasmine_Angularjs Controller - Fatal编程技术网

无法在ANgularJS中为控制器编写Jasmine测试用例

无法在ANgularJS中为控制器编写Jasmine测试用例,angularjs,jasmine,angularjs-controller,Angularjs,Jasmine,Angularjs Controller,我的控制器中有以下代码,我想为这一部分编写Jasmine测试用例。 我试着写一个,但它有以下错误 TypeError:对象[Object Array]没有方法“then” 控制器代码:: $scope.doGetList = function() { var queryString = {......sending some query parameters}; searchResource.getList(queryString)

我的控制器中有以下代码,我想为这一部分编写Jasmine测试用例。 我试着写一个,但它有以下错误 TypeError:对象[Object Array]没有方法“then”

控制器代码::

$scope.doGetList = function() {
                var queryString = {......sending some query parameters};
                searchResource.getList(queryString).then(
                    function (data) {
                        $scope.sugesstions = data;
                    }
                );
            };
Jasmine测试用例::

it("should return provided list", angular.mock.inject(function($rootScope, $controller) {
                            var scope = $rootScope.$new();

                            var searchResource = {
                                getList: function() {
                                    return ['suggestions1', 'suggestions2', 'suggestions3'];
                                }
                            };

                            $controller(
                                    headerController,
                                    {
                                        $scope: scope,
                                        cartsService: null,
                                        currentUser: null,
                                        searchResource: searchResource
                                    }
                            );

                            expect(scope.sugesstions).toBeDefined();
                            expect(scope.sugesstions.length).toBe(0);

                            //this method will call mock method instead of actual server call
                            scope.doGetAutocomplete();
                            expect(scope.sugesstions.length).toBe(3);
                            expect(scope.sugesstions[0]).toEqual('suggestions1');
                            expect(scope.sugesstions[1]).toEqual('suggestions2');
                            expect(scope.sugesstions[2]).toEqual('suggestions3');
                        }));

我应该如何编写它。

您必须在runs()中包装异步调用。来自jasmine doc:


或者,我像承诺的那样使用茉莉花,并提供更好的支持:

我不确定如何使用它。如果有任何示例代码,那么这将是有帮助的。两个链接中不是都有示例吗?但是这些示例是不同的,对这个场景没有太大帮助。我仍然无法解决这个问题。。。有什么建议吗??