ReferenceError:无法在AngularJS,Karma中找到变量:xxxService

时间:2016-04-04 19:53:35

标签: javascript angularjs unit-testing jasmine karma-jasmine

我不熟悉angularJS和Karma / Jasmine的单元测试。

我正在尝试将单元测试添加到现有的应用程序中。其中一项服务如下所示:



angular.module('fundMetadata').factory('fundMetadataService', [
    'symedUIGridService',
    'symEdService',

    function(symedUIGridService, symEdService) {

        var $scope;

        function setCtrl(controller, scope) {
            ctrl = controller;
            $scope = scope;
        }

        function defaultGridOptions(columns, data) {
            return (symedUIGridService.defaultGridOptionsGenerator(
                $scope, ctrl))(columns, data);
        }
      
      
        function equityOptions() {
            var options = defaultGridOptions(ctrl.fundMetadataResponse.equityColumns,
                ctrl.fundMetadataResponse.equityRecords);
            options.onRegisterApi = undefined;
            return options;
        }


        return {
            equityOptions: equityOptions,
            setCtrl: setCtrl
        }
    }
]);




然后在karma / jasmine进行以下测试:



describe('fundMetadata', function () {
    beforeEach(module('fundMetadata'));
    beforeEach(function () {
        module('fundMetadata');
        inject(function (_fundMetadataService_) {
            fundMetadataService = _fundMetadataService_;
        });
    });

    it('should have the functions', function () {
        expect(angular.Function(fundMetadataService.setCtrl)).toBe(true);
    });

 });





PhantomJS 2.1.1 (Windows 7 0.0.0) fundMetadata should have the functions FAILED
        forEach@C:/Users/rvendiola/proj/SymbologyEditor/Website/bower_components/angular/angular.js:321:24
        loadModules@C:/Users/rvendiola/proj/SymbologyEditor/Website/bower_components/angular/angular.js:4508:12
        createInjector@C:/Users/rvendiola/proj/SymbologyEditor/Website/bower_components/angular/angular.js:4430:30
        workFn@C:/Users/rvendiola/proj/SymbologyEditor/Website/bower_components/angular-mocks/angular-mocks.js:2922:60
        inject@C:/Users/rvendiola/proj/SymbologyEditor/Website/bower_components/angular-mocks/angular-mocks.js:2902:46
        C:/Users/rvendiola/proj/SymbologyEditor/Website/t/fundMetadataService.js:7:15
        C:/Users/rvendiola/proj/SymbologyEditor/Website/bower_components/angular/angular.js:4548:53
        ReferenceError: Can't find variable: fundMetadataService in C:/Users/rvendiola/proj/SymbologyEditor/Website/t/fundMetadataService.js (line 13)
        C:/Users/rvendiola/proj/SymbologyEditor/Website/t/fundMetadataService.js:13:52




我做了一个简单的测试,测试app.js并且工作正常,所以我认为我的装置很好。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

应该有这个功能。意思是,应该定义函数

it('expects setCtrl to be defined', function () {
    expect(fundMetadataService.setCtrl).toBeDefined();
});

“angular.Function”不是“ thing