加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 使用.success()获取’未定义’的Karma测试不是对象

发布时间:2020-12-17 17:45:26 所属栏目:安全 来源:网络整理
导读:我正在尝试编写一个单元测试,看看如果适当设置某些属性,我的控制器中的’getStudents()’提供程序函数是否会被调用.注意.success()回调: $scope.update = function update() { // omitted,just doing some checking... // finally else if (key.length ===
我正在尝试编写一个单元测试,看看如果适当设置某些属性,我的控制器中的’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,MobilePhone: "7777777"
            }]
        }];
    }
};

有了这个:

var mockStudentsProvider = {
    getStudents: function getStudents() {
        var retVal = [{
            Education: [],MobilePhone: "7777777"
            }]
        }];
        return {
            success: function(fn) {
                fn(retVal)
            };

        }
    }
};

并替换这个:

spyOn(StudentsProvider,'getStudents');

有了这个:

spyOn(StudentsProvider,'getStudents').andCallThrough();

>当您不使用andCallThrough()或andCallFake()时,jasmine会阻止执行该方法并返回null.在您的更新方法中,您调用null.success.这将失败. (http://jasmine.github.io/1.3/introduction.html)
>在模拟方法中,您需要更改返回格式 – 真正的http方法返回一个对象,其中成功是指一个输入回调函数的函数.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读