简体   繁体   English

Angularjs单元测试 - 要定义的函数

[英]Angularjs Unit Test - function to be defined

How do i check whether a particular function is defined or not? 如何检查特定功能是否已定义?

I am using following unit test, doesn't work. 我使用以下单元测试,不起作用。

controller 调节器

$scope.filterData = function () {
        $scope.currentPage = "1";
        $scope.displayData($scope.currentPage);
        ......
    }

test 测试

var ctrl, scope;

beforeEach(function () {
    // load the module
    module('myApp');
    // inject Controller for testing
    inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        ctrl = $controller('DataCtrl', { $scope: scope });
    });
});
it('should have a filterData function to be defined', function () {
    expect(scope.filterData()).toBeDefined();
});

Remove the braces like this: 删除这样的大括号:

it('should have a filterData function to be defined', function () {
   expect(scope.filterData).toBeDefined();
});

Since you dont wanna execute the function itself, but only check if its there. 既然你不想执行函数本身,但只检查它是否存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM