javascript - Angular 和 Jasmine - $scope 函数未定义

标签 javascript angularjs jasmine

我有一个 Controller

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


});

我想测试 Controller 方法是否正常工作,但 jasmine 会在第二次测试时返回错误:

TypeError: undefined is not a function

导致问题的是 $scope.setPageSize(12)。我正在关注有关 Angular 文档的教程,他们正在使用范围的方法,但在我的情况下不起作用。有谁知道为什么?

最佳答案

我想你应该注意到在 jasmine 中,'it' 'describe' 是函数。

因此,当您需要在 $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 document 中查看.

希望这会奏效。 :)

关于javascript - Angular 和 Jasmine - $scope 函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27777046/

相关文章:

javascript - Protractor 在描述函数之间传递变量

使用 clientWidth 时 AngularJS 单元测试错误

javascript - rake 数据库 :create - Could not find a JavaScript runtime

javascript - 是否可以根据配置更改在 AngularJS 中使用不同的模块?

javascript - 如果没有数据则不渲染元素

javascript - AngularJS 指令还是服务?

javascript - ECMAScript 模板文字返回不同的结果

javascript - 在react-native中加载launchChrome.js时出现语法错误

javascript - 当 bool 变为 true 时,如何触发函数调用?

javascript - Jasmine - 监视 EXTJS 监听器中调用的函数