How do I test an AngularJS service with Jasmine?

后端 未结 4 592
长发绾君心
长发绾君心 2020-12-04 06:37

(There is a related question here: Jasmine test does not see AngularJS module)

I just want to test a service without bootstrapping Angular.

I have look at so

4条回答
  •  余生分开走
    2020-12-04 06:42

    If you wanna test a controller you can inject and test it as below.

    describe('When access Controller', function () {
        beforeEach(module('app'));
    
        var $controller;
    
        beforeEach(inject(function (_$controller_) {
            // The injector unwraps the underscores (_) from around the parameter names when matching
            $controller = _$controller_;
        }));
    
        describe('$scope.objectState', function () {
            it('is saying hello', function () {
                var $scope = {};
                var controller = $controller('yourController', { $scope: $scope });
                expect($scope.objectState).toEqual('hello');
            });
        });
    });
    

提交回复
热议问题