Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.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 使用.success()获取';未定义';不是对象';_Angularjs_Unit Testing_Karma Runner - Fatal编程技术网

Angularjs 使用.success()获取';未定义';不是对象';

Angularjs 使用.success()获取';未定义';不是对象';,angularjs,unit-testing,karma-runner,Angularjs,Unit Testing,Karma Runner,我正在尝试编写一个单元测试,以查看如果某些属性设置正确,是否会调用控制器中的“getStudents()”提供程序函数。请注意.success()回调: $scope.update = function update() { // omitted, just doing some checking... // finally else if (key.length === 3 || $scope.students.length === 0) { Stud

我正在尝试编写一个单元测试,以查看如果某些属性设置正确,是否会调用控制器中的“getStudents()”提供程序函数。请注意.success()回调:

 $scope.update = function update() {
    // omitted, just doing some checking...
    // finally 
    else if (key.length === 3 || $scope.students.length === 0) {
       StudentsProvider.getStudents($scope.keyword, $scope.selectedFilters).success(function(data) {
           $scope.students = data;
       });
    }
 };
我的业力单元测试如下所示:

describe("Students: Controllers", function () {
    var $scope;
    var ctrl;
    beforeEach(module('studentsApp'));

    describe("SearchCtrl", function () {
        // Mock the provider
        var mockStudentsProvider = {
            getStudents: function getStudents() {
                return [
                    {
                         Education: [], 
                         Person: [{ 
                             ID: 1, 
                             Name: "Testing McTestsson", 
                             SSN: "1234567890",
                             Address: "Fakestreet 3", MobilePhone: "7777777"
                         }]
                    }
                ];
             }
         };
         var StudentsProvider;
         beforeEach(inject(function ($controller, $rootScope) {
             $scope = $rootScope.$new();
             ctrl = $controller('SearchCtrl', { $scope: $scope, StudentsProvider: mockStudentsProvider});
             StudentsProvider = mockStudentsProvider;
         }));
         describe("Update", function () {
             beforeEach(function () {
                 spyOn(StudentsProvider, 'getStudents');
             });
             it("should always call the provider with 3 letters", function () {
                 $scope.keyword = "axe";
                 $scope.update();
                 expect(StudentsProvider.getStudents).toHaveBeenCalled();
                 expect(StudentsProvider.getStudents).toHaveBeenCalledWith("axe", "");
             });
         });
    });
});
当我运行此操作时,会出现以下错误:

TypeError: 'undefined' is not an object (evaluating 'StudentsProvider.getStudents($scope.keyword, $scope.selectedFilters).success')
这可能是因为我没有嘲笑.success()回调函数。我该怎么做?提前谢谢

替换这个:

var mockStudentsProvider = {

    getStudents: function getStudents() {
        return [{
            Education: [],
            Person: [{
                ID: 1,
                Name: "Testing McTestsson",
                SSN: "1234567890",
                Address: "Fakestreet 3",
                MobilePhone: "7777777"
            }]
        }];
    }
};
spyOn(StudentsProvider, 'getStudents');
为此:

var mockStudentsProvider = {
    getStudents: function getStudents() {
        var retVal = [{
            Education: [],
            Person: [{
                ID: 1,
                Name: "Testing McTestsson",
                SSN: "1234567890",
                Address: "Fakestreet 3",
                MobilePhone: "7777777"
            }]
        }];
        return {
            success: function(fn) {
                fn(retVal)
            };

        }
    }
};
spyOn(StudentsProvider, 'getStudents').andCallThrough();
并将其替换为:

var mockStudentsProvider = {

    getStudents: function getStudents() {
        return [{
            Education: [],
            Person: [{
                ID: 1,
                Name: "Testing McTestsson",
                SSN: "1234567890",
                Address: "Fakestreet 3",
                MobilePhone: "7777777"
            }]
        }];
    }
};
spyOn(StudentsProvider, 'getStudents');
为此:

var mockStudentsProvider = {
    getStudents: function getStudents() {
        var retVal = [{
            Education: [],
            Person: [{
                ID: 1,
                Name: "Testing McTestsson",
                SSN: "1234567890",
                Address: "Fakestreet 3",
                MobilePhone: "7777777"
            }]
        }];
        return {
            success: function(fn) {
                fn(retVal)
            };

        }
    }
};
spyOn(StudentsProvider, 'getStudents').andCallThrough();
  • 当您不使用
    和callthrough()
    和callfake()
    时,jasmine会阻止方法的执行并返回null。在更新方法中,您正在调用
    null.success
    。这将失败。()

  • 在mock方法中,您需要更改返回格式——真正的http方法返回一个对象,其中success引用一个函数,该函数将输入作为回调函数

  • 在您的情况下,回调函数是:

    function(data) {
       $scope.students = data;
    }