简体   繁体   English

茉莉花单元测试$ scope访问

[英]Jasmine unit test $scope access

Why am I unable to access my function in my Controller? 为什么我无法在Controller中访问我的功能? The code functions like I would expect it too, however, it doesn't seem to want to allow me access to my function that I'm trying to unit test. 代码功能也像我期望的那样,但是,它似乎不想让我访问我要进行单元测试的函数。 It should just return a simple bool, but it's getting killed somewhere. 它应该只返回一个简单的布尔值,但是在某个地方被杀死了。

Here's some code: 这是一些代码:

RTHelper.js 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 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;
        }
    }

How would I go about getting to that BoolCondition? 我将如何去那个BoolCondition? I'm new to this so you can imagine the struggle of writing unit tests after never having done unit testing. 我对此并不陌生,因此您可以想象从未进行过单元测试之后编写单元测试的过程。 I've also gone through countless examples and I've done some generic tests, so I'm not totally un-versed. 我也经历了无数的示例,并且已经进行了一些通用测试,因此我并非一无所知。

You're not bootstrapping the module under test correctly. 您没有正确引导被测模块。 You should use angular.mock.module inside the test 您应该在测试中使用angular.mock.module

You're not instantiating the controller (where's the call to ctrl()?) 您没有实例化控制器(对ctrl()的调用在哪里?)

Here's the complete working example and the 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();
    });
})

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

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