用.success()获取'未定义'的Karma测试不是一个对象'

时间:2014-02-26 23:39:27

标签: 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) {
       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()回调。我该怎么办?提前谢谢!

1 个答案:

答案 0 :(得分:7)

替换它:

var mockStudentsProvider = {

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

用这个:

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');

用这个:

spyOn(StudentsProvider, 'getStudents').andCallThrough();
  1. 当您不使用andCallThrough()andCallFake()时,jasmine会阻止执行该方法并返回null。在您的更新方法中,您正在调用null.success。这将失败。 (http://jasmine.github.io/1.3/introduction.html

  2. 在你的mock方法中你需要改变返回格式 - 真正的http方法返回一个对象,其中success指的是一个输入回调函数的函数。

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

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