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
Javascript 角度和Jasmine-$范围函数未定义_Javascript_Angularjs_Jasmine - Fatal编程技术网

Javascript 角度和Jasmine-$范围函数未定义

Javascript 角度和Jasmine-$范围函数未定义,javascript,angularjs,jasmine,Javascript,Angularjs,Jasmine,我有一个控制器 var videoApp = angular.module('videoApp', ['videoAppFilters', 'ui.unique', 'angularUtils.directives.dirPagination']); videoApp.controller('VideoCtrl', function ($scope, $http, $filter, cacheLoader, $rootScope) { $scope.setPageSize = func

我有一个控制器

var videoApp = angular.module('videoApp', ['videoAppFilters', 'ui.unique', 'angularUtils.directives.dirPagination']);
videoApp.controller('VideoCtrl', function ($scope, $http, $filter, cacheLoader, $rootScope) {

    $scope.setPageSize = function (pageSize) {
        $scope.pageSize = pageSize;
        return $scope.pageSize;
    };

    $scope.addFavorite = function (data, key) {
        localStorage.setItem(key, data);
        $scope.videos = $filter('articleFilter')(data, $scope.allData);
        return alert(key + " "+ data + " was added to your favorite list.");
    };

    $scope.addSelectedClass = function (event) {
        if($(event.target).hasClass("selected") == true)
        {
            $(event.target).removeClass("selected");
        } else {
            $(".selected").removeClass("selected");
            $(event.target).addClass("selected");
        }
    };

    $scope.filterArticles = function (category) {
        $scope.videos = $filter('articleFilter')(category, $scope.allData);
    };

    $scope.pageSize = 12;

    cacheLoader.load('http://academy.tutoky.com/api/json.php', true, function () {
        $scope.allData = $rootScope.allData;
        $scope.videos = $rootScope.videos;

        if(localStorage.getItem('category')) {
            $scope.videos = $filter('articleFilter')(localStorage.getItem('category'), $scope.allData);
        } else {
            $scope.videos = data;
        }
    });

});
还有一个测试

describe('Check if VideoListCtrl', function() {
    beforeEach(module('videoApp'));

    beforeEach(inject(function (_$controller_) {
        $controller = _$controller_;
    }));

    beforeEach(inject(function ($rootScope) {
        $scope = {};
        controller = $controller('VideoCtrl', { $scope: $scope });
    }));

    it('exists', function() {
        expect(controller).not.toBeNull();
    });

    it('set page size', function () {
        expect($scope.setPageSize(12)).toEqual(12);
    });


});
我想测试控制器方法是否正常工作,但jasmine将在第二次测试中返回错误:

TypeError:undefined不是函数


导致问题的原因是$scope.setPageSize12。我在学习angular docs的教程,他们使用的是类似于此的范围方法,但在我的案例中它不起作用。有人知道为什么吗?

我想你应该注意到在jasmine中,“it”“description”是函数

因此,当您需要在$scope内测试某些内容时。你应该确保 在任何“it”函数中,它都应该访问变量

只需将您的测试用例更改为:

describe('Check if VideoListCtrl', function() {
    //Add an initialize here:
    var $scope = null;
    beforeEach(module('videoApp'));

    beforeEach(inject(function (_$controller_) {
        $controller = _$controller_;
    }));

    beforeEach(inject(function ($rootScope) {
        //new a $scope
        $scope = $rootScope.$new();
        controller = $controller('VideoCtrl', { $scope: $scope });
    }));

    it('exists', function() {
        expect(controller).not.toBeNull();
    });

    it('set page size', function () {
        expect($scope.setPageSize(12)).toEqual(12);
    });
});
或者你可以登记


希望这能奏效

我想你应该注意到,在jasmine中,“it”“description”是函数

因此,当您需要在$scope内测试某些内容时。你应该确保 在任何“it”函数中,它都应该访问变量

只需将您的测试用例更改为:

describe('Check if VideoListCtrl', function() {
    //Add an initialize here:
    var $scope = null;
    beforeEach(module('videoApp'));

    beforeEach(inject(function (_$controller_) {
        $controller = _$controller_;
    }));

    beforeEach(inject(function ($rootScope) {
        //new a $scope
        $scope = $rootScope.$new();
        controller = $controller('VideoCtrl', { $scope: $scope });
    }));

    it('exists', function() {
        expect(controller).not.toBeNull();
    });

    it('set page size', function () {
        expect($scope.setPageSize(12)).toEqual(12);
    });
});
或者你可以登记


希望这能奏效

您使用的是哪个版本的Angular?此案例适用于1.3.x。请添加您得到的完整错误。我使用1.3.7版本。不知道为什么它不工作…使用哪个版本的Angular?此案例适用于1.3.x。请添加您得到的完整错误。我使用1.3.7版本。不知道为什么它不工作。。。