search

Home  >  Q&A  >  body text

angular.js - [$injector:unpr] Unknown provider problem always occurs when injecting angular-test

Hello, the following is the problem I encountered:
But in the official document, it mentions the inject step beforeEach, and when I write it using the official method, it always displays unknown ControllerProvider. Error,
The following is the code I wrote according to the official documentation:

describe('indexCtrl',function(){
        var $controller;
        beforeEach(module('controllers'));
        beforeEach(inject(function(_controller_){
            $controller = _controller_;
        }));

        describe('the add function should be right',function(){
            it('should be 34',function(){
                var $scope = {};
                var controller = $controller('indexCtrl',{$scope: $scope});
                $scope.add(14,20);
                except(controller).toBeDefined();
                except($scope.sum).toEqual(34);
            })
        })
})

The following is the error reported:

    Error: [$injector:unpr] Unknown provider: controllerProvider <- controll

er

    http://errors.angularjs.org/1.5.8/$injector/unpr?p0=controllerProvider%2

0<- controller

The following is the code I modified based on an example on the Internet:

it('add test', inject(function ($controller) {
            var $scope = {};
            //spec body
            var indexCtrl = $controller('indexCtrl', {$scope: $scope});
            expect(indexCtrl).toBeDefined();
            expect($scope.add(2, 3)).toEqual(5);
 }));
    测试通过

I don’t know the reason, but it stands to reason that what is given on the official website is correct. The two codes are just injected in a different order. Hope everyone can help

滿天的星座滿天的星座2817 days ago627

reply all(1)I'll reply

  • 某草草

    某草草2017-05-15 17:10:32

    I have the same problem. Please tell me what went wrong and how to solve it?

    reply
    0
  • Cancelreply