【问题标题】:Jasmine unit test $scope accessJasmine 单元测试 $scope 访问
【发布时间】:2014-06-10 16:16:16
【问题描述】:

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

这里有一些代码:

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?我是新手,因此您可以想象在从未进行过单元测试之后编写单元测试的困难。我也经历了无数的例子,我做了一些通用的测试,所以我并不完全不精通。

【问题讨论】:

    标签: javascript angularjs unit-testing jasmine


    【解决方案1】:

    您没有正确引导被测模块。您应该在测试中使用 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();
        });
    })
    

    【讨论】:

    • 嘿 Eitan,感谢您的回复,但是,我仍然收到一个非常烦人的错误。错误:[$injector:modulerr] errors.angularjs.org/1.2.2/$injector/moduler 我知道它是正确的,因为我杀死了它并且可以看到我的函数,但是我无法在我的代码中到达那里。你对这个错误有什么提示吗?我发现的结果相当缺乏......
    • 整个错误是什么?它缺少哪个模块?它也可能与您的跑步者配置有关,也请查看
    • 它显示为导致任何类型问题的唯一文件是: at file: /www/js/angular/angular.min.js:6:449 at file: /www/js/angular /angular.min.js:29:279 at Array.forEach (native) at q (file: /www/js/angular/angular.min.js:7:255) at e (file: /www/js/angular /angular.min.js:28:427)在 Object.Yb [作为注入器](文件:/js/angular/angular.min.js:32:462)在 workFn(文件:/js/angular/angular-mocks .js:2101:52) 在 window.inject.angular.mock.inject (文件: /js/angular/angular-mocks.js:2092:30) 在 Object. (/spec/RTHelper.js:67 :9)
    • 首先,您应该使用调试 angular.js 版本(未缩小)运行单元测试以获得可读的堆栈跟踪,其次,您丢失模块的实际名称在哪里?
    • 好的,对于第一个,我在哪里可以找到那个文件?另外,我的 specRunner 加载了这个文件: app.js angular.module('TDE', []); var TDE = angular.module('TDE', ['ux', 'ngRoute', 'ngResource', 'TDE', 'hmTouchEvents', '无限滚动', 'ui.bootstrap']);显然现在没有加载......所以我要开始了
    猜你喜欢
    • 2015-10-12
    • 2016-09-14
    • 2015-10-26
    • 1970-01-01
    • 2014-09-30
    • 2017-04-04
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多