Jasmine单元测试$ scope访问

时间:2014-06-10 16:16:17

标签: javascript angularjs unit-testing jasmine

为什么我无法在控制器中访问我的功能?代码功能也像我期望的那样,但是,它似乎并不想让我访问我的功能,我试图进行单元测试。它应该只返回一个简单的布尔,但它在某个地方被杀死。

这里有一些代码:

RTHelper.js

describe('Unit: LocationController', function () {
    var $scope, $httpBackend, $location, injector, ctrl, $controller;
    //beforeEach(function () {
    //    angular.module('TDE').controller('LocationController'); //
    //    inject(function ($injector) {
    //        $rootScope = $injector.get('$rootScope');
    //        $scope = $rootScope.$new();
    //        //ctrl = $injector.get('$controller')("LocationController", { $scope: $scope });
    //        injector = $injector;
    //        ctrl = $injector.get('$controller');
    //        //scope = $injector.get('$rootScope').$new();
    //        $httpBackend = $injector.get('$httpBackend');
    //        $location = $injector.get('$location');
    //    });
    //});

    //both beforeEach methods work(which one is better? I don't know), so things are getting loaded
    beforeEach(function () {
        angular.module('TDE');
        inject(function ($injector) {
            $location = $injector.get('$location');
            $rootscope = $injector.get('$rootScope');
            $scope = $rootscope.$new();

            $controller = $injector.get('$controller');

            ctrl = function () {
                return $controller('LocationController', {
                    '$scope': $scope
                })
            };
        })
    });
    it("should just be a holder for something for later", function () {
        expect($scope.BoolCondition()).toBeDefined(); //I don't care what it returns as long as it's accessed honestly
    });  
})

LocationController.js

    angular
    .module('TDE')
    .controller('LocationController', ['$rootScope', '$scope', '$location', '$window', '$document', 'LocationService', 'HeaderFooterService', 'SearchService', 'TranslationService', 'MTDE_CONFIG', 'LocationPartnerAssignmentService', 'ExperimentService', function ($rootScope, $scope, $location, $window, $document, $LocationService, $HeaderFooterService, $SearchService, $TranslationService, $MTDE_CONFIG, $LocationPartnerAssignmentService, $ExperimentService) {

    $scope.BoolCondition = function(myCondition){
        if(//blah blah condition test on myCondition)
        {
            return true
        }
        else
        {
            return false;
        }
    }

我将如何进入BoolCondition?我是新手,所以你可以想象从未完成单元测试后编写单元测试的困难。我也经历了无数的例子,并且我做了一些通用的测试,所以我并不完全不了解。

1 个答案:

答案 0 :(得分:0)

您没有正确地引导正在测试的模块。你应该在测试中使用angular.mock.module

你没有实例化控制器(在哪里调用ctrl()?)

以下是完整的工作示例和fiddle that runs it

    describe('Unit: LocationController', function () {
    var $scope, $location, ctrl;
    beforeEach(function () {
        angular.mock.module('TDE');
        inject(function (_$location_, _$rootScope_, _$controller_) {
            $location = _$location_;
            $scope = _$rootScope_.$new();

            ctrl = _$controller_('LocationController', {
                '$scope': $scope
            })
        });
    });
    it("should just be a holder for something for later", function () {
        expect($scope.BoolCondition()).toBeDefined();
        expect($scope.BoolCondition()).toBeTruthy();
    });
})