Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Unit testing 安格拉斯,茉莉,沙巴。。。单元测试过滤器?_Unit Testing_Angularjs_Jasmine_Chutzpah - Fatal编程技术网

Unit testing 安格拉斯,茉莉,沙巴。。。单元测试过滤器?

Unit testing 安格拉斯,茉莉,沙巴。。。单元测试过滤器?,unit-testing,angularjs,jasmine,chutzpah,Unit Testing,Angularjs,Jasmine,Chutzpah,我只是想通过AngularJS更好地理解Jasmine单元测试。因此,我围绕我开始的一个现有实验项目设置了一个测试。我将从代码开始,我的问题在底部 首先,我的应用声明: (function () { "use strict"; angular.module('iconic', []) .constant('config', { debug: true, version: '1.0.0.1' }) .value('globalStatus',

我只是想通过AngularJS更好地理解Jasmine单元测试。因此,我围绕我开始的一个现有实验项目设置了一个测试。我将从代码开始,我的问题在底部

首先,我的应用声明:

(function () {
"use strict";
angular.module('iconic', [])
    .constant('config', {
        debug: true,
        version: '1.0.0.1'
    })
    .value('globalStatus', {            
        currentArea: null,
        progress: null,
        notice: [
            { title: 'Notice 1 Title', message: 'Notice 1 Message' },
            { title: 'Notice 2 Title', message: 'Notice 1 Message' }
        ]
    });
}());
然后是一个获取数据的工厂(现在是静态的,但将是一个AJAX调用):

然后我的控制器与过滤器:

(function () {
"use strict";
angular.module('iconic')
    .controller('NavController', ['$scope', 'data', function ($scope, data) {
            $scope.menus = data.getAreas();
        }])
    .filter('EnabledFilter', ['config', function (config) {
            return function (menus) {
                if (config.debug)
                    console.log('matchEnabled', arguments);
                var filtered = [];
                angular.forEach(menus, function (menu) {
                    if (menu.enabled) {
                        filtered.push(menu);
                    }
                });
                return filtered;
            };
        }]);
}());
然后是我实际的Jasmine测试(用Chutzpah运行此测试):

因此,这是第一个简单的测试,以确保getAreas被调用的过程非常顺利。但我想添加一个测试,基本上确保过滤结果从工厂过滤出数据,其中enabled为false。你知道我该怎么跟茉莉花一起做吗

(function () {
"use strict";
angular.module('iconic')
    .controller('NavController', ['$scope', 'data', function ($scope, data) {
            $scope.menus = data.getAreas();
        }])
    .filter('EnabledFilter', ['config', function (config) {
            return function (menus) {
                if (config.debug)
                    console.log('matchEnabled', arguments);
                var filtered = [];
                angular.forEach(menus, function (menu) {
                    if (menu.enabled) {
                        filtered.push(menu);
                    }
                });
                return filtered;
            };
        }]);
}());
(function () {
"use strict";

var staticData = [
    { name: 'home', text: 'Home', enabled: true, active: false },
    { name: 'gallery', text: 'Gallery', enabled: true, active: false },
    { name: 'services', text: 'Services', enabled: true, active: false },
    { name: 'pricing', text: 'Pricing', enabled: true, active: false },
    { name: 'test', text: 'Test', enabled: false, active: false }
];

describe("NavController Tests", function () {
    //Mocks
    //Mocks
    var windowMock, httpBackend, _data;

    //Controller
    var ctrl;

    //Scope
    var ctrlScope;

    //Data
    var storedItems;

    beforeEach(function () {
        module('iconic');
    });

    beforeEach(inject(function ($rootScope, $httpBackend, $controller, data) {

        //Mock for $window service
        windowMock = { location: { href: "" } };


        //Creating a new scope
        ctrlScope = $rootScope.$new();

        //Assigning $httpBackend mocked service to httpBackend object
        httpBackend = $httpBackend;

        _data = data;

        storedItems = staticData;

        //Creating spies for functions of data service
        spyOn(data, 'getAreas').andCallThrough();

        $controller('NavController', { $scope: ctrlScope, data: _data});
    }));

    it("should call getAreas on creation of controller", function () {
        expect(_data.getAreas).toHaveBeenCalled();
    });
});

}());