【问题标题】:Testing angularjs with dependencies使用依赖项测试 angularjs
【发布时间】:2016-11-24 20:30:11
【问题描述】:

我对在 AngularJS 中进行测试还很陌生,而且我很困惑。我有一个控制器,它依赖于一个依赖于一个值的服务。我已经为控制器编写了非常基本的测试,只是为了掌握它,当我没有将值注入服务时,它们都通过了。但是,当我将值注入服务时,控制器的所有测试都会出现Unknown provider 错误。

我的代码如下:

控制器

'use strict';
angular.module('app')
    .controller('testController', function($scope, testService) {

        $scope.$on('$ionicView.enter', function() {
            $scope.setup();
        });

        $scope.testFunction = function() {
            $scope.result = 1;
        };

        $scope.setup = function() {
            testService.superAwesomeFunction();
            $scope.enterValue = 1;
        };

    });

服务

'use strict';
angular.module('app')
    .service('testService', ['URLRoute', function(URLRoute) {

        var superAwesomeFunction = function() {
            var URLRequest = URLRoute;
            console.log(URLRequest);
        };

        return {
            superAwesomeFunction: superAwesomeFunction
        };
    }]);

价值

'use strict';
angular.module('app')
    .value('URLRoute', {
        url: 'https://abcdefghijklmnop.com/api',
    });

test.controller.test.js

'use strict';
describe('testController', function() {
    beforeEach(module('app'));

    var $controller, 
        $rootScope, 
        $scope;

    beforeEach(inject(function(_$controller_, _$rootScope_) {
        $controller = _$controller_;
        $rootScope = _$rootScope_;
        $scope = $rootScope.$new();

        $controller('testController', { 
            $scope: $scope
        });
    }));

    describe('$scope.$on(ionicView.enter)', function() {
        it ('$scope.$on calls $ionicView.enter', function() {
            spyOn($scope, '$on').and.callThrough();
            $scope.$on('$ionicView.enter');
            expect($scope.$on).toHaveBeenCalledWith('$ionicView.enter');
        });
    });

    describe('$scope.testFunction', function() {
        it('Sets the value of result to 1', function() {
            $scope.testFunction();
            expect($scope.result).toEqual(1);
        });
    });

    describe('$scope.setup', function() {
        it('Sets up stuff for the view after it has loaded', function() {
            $scope.setup();
            expect($scope.enterValue).toEqual(1);
        });
    });


});

现在运行测试时,我得到的错误是:

Unknown provider: URLRouteProvider <- URLRoute <- testService

任何帮助表示赞赏,对此感到非常困惑。

【问题讨论】:

    标签: angularjs unit-testing ionic-framework jasmine karma-jasmine


    【解决方案1】:

    试试这个:

    'use strict';
    angular.module('app')
    .service('testService', [ function(URLRoute) {
    
        var superAwesomeFunction = function() {
            var URLRequest = URLRoute;
            console.log(URLRequest);
        };
    
        return {
            superAwesomeFunction: superAwesomeFunction
        };
    }]);
    

    来自:lostechies

    【讨论】:

    • 是的,所以这不是有效的 Angular,但我认为你的意思是去掉方括号以缩小它并以这种方式尝试?如果是这样,它会导致相同的错误
    猜你喜欢
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多