简体   繁体   English

错误:喷油器已创建,无法注册模块! 角1.2茉莉1.3.0

[英]Error: Injector already created, can not register a module! Angular 1.2 jasmine 1.3.0

i'm writing a small application in angular and jasmine 1.3 for my test. 我正在为我的Angle和Jasmine 1.3编写一个小型应用程序。

First test is working fine, but i have a situation with the second one. 第一次测试工作正常,但第二次测试我遇到了问题。

here is a snippet of code with witch i'm struggling: 这是我正在努力的女巫的代码片段:

describe('helperFactoryTest', function() {

    var helperFactory = null,
        items = [
            {id : 1, item : 'Apples', qty : 2, type : 2, done : 1 },
            {id : 2, item : 'Bread', qty : 1, type : 1,  done : 1 },
            {id : 3, item : 'Bananas', qty : 5, type : 2,  done : 0 },
            {id : 4, item : 'Pears', qty : 8, type : 2,  done : 0 }
        ],

        thisItems = [];

    beforeEach(function() {

        module('myApp');

        inject(function(_helperFactory_) {

            helperFactory = _helperFactory_;

        });
    });


    it('should filter array and return records that are completed (done)',
        function() {

            thisItems = helperFactory.filterFieldArrayByDone(items, 'id', 1);

            expect(thisItems.length).toEqual(2);

        }
    );


describe('ShoppingLIstController helper methods test', function() {

    var $scope,
        helperFactory,
        ShoppingListController;


    beforeEach(function() {

        module('myApp');

        inject(function($rootScope, _helperFactory_, $controller) {

           $scope = $rootScope.$new();

           helperFactory = _helperFactory_;

           ShoppingListController = $controller('ShoppingListController', {

               $scope : $scope,
               helperFactory : helperFactory

           });

        });

    });


    it('should return 0 for 2 characters', function() {

        $scope.item = '12';

        expect($scope.howManyMoreCharactersNeeded()).toEqual(0);

    });


});



});

Here is the function that i'm calling in my test: 这是我在测试中调用的函数:

$scope.howManyMoreCharactersNeeded = function() {

                var characters = (MIN_LENGTH - $scope.item.length);

                return (characters > 0) ? characters : 0;

            };

MIN_LENGTH is equal to 2; MIN_LENGTH等于2;

$scope.item.length is the length of characters in textfield; $ scope.item.length是文本字段中字符的长度;

Can you please tell me what i'm doing wrong. 你能告诉我我在做什么错。

From my understanding and after coming across exactly similar problem, what i have understood is that , The "modules" that need to be mocked into tests are supposed to be placed at the top, before any other "inject()", that is used to handle special dependencies. 根据我的理解,在遇到了完全类似的问题之后,我了解到, 应该将需要模拟的“模块”放置在使用任何其他“ inject()”之前的顶部。处理特殊的依赖关系。

So, considering your case , you have a nested suite (describe('ShoppingLIstController helper methods test') ) , where, you are trying to mock the module again. 因此,考虑到您的情况,您有一个嵌套套件(describe('ShoppingLIstController helper methods test')) ,在这里,您尝试再次模拟该模块

I presume, that you might have forgot to close the first suite ( ie, 'helperFactoryTest' ) , which evidently made your next suite as the nested one. 我想,您可能忘记了关闭第一个套件(即'helperFactoryTest') ,这显然会使您的下一个套件成为嵌套套件。 So, clearly analysing your suite's opening and closing brackets and terminating your 1st suite solve the error you have come across. 因此,清楚地分析套件的起始和闭合括号并终止第一个套件可以解决您遇到的错误。

If that is not the case, and if you are into the nested suite , then, note the comments from the below code , which points out the cause of the problem. 如果不是这种情况,并且您属于嵌套套件 ,那么请注意以下代码中的注释,指出了问题的原因。

 describe('helperFactoryTest', function() {                 // Parent suite
   var helperFactory = null,
       items = [
            {id : 1, item : 'Apples', qty : 2, type : 2, done : 1 },
                   .
                   .
            {id : 4, item : 'Pears', qty : 8, type : 2,  done : 0 }
    ],
    thisItems = [];
    beforeEach(function() {
        module('myApp');                      //  First Module Mock

        inject(function(_helperFactory_) {    // First inject()
            helperFactory = _helperFactory_;
        });
    });
    it('should filter array and return records that are completed (done)',
        function() {
            //Some test       
        }
    );
    describe('ShoppingLIstController helper methods test', function() {  // Nested Suite
    var $scope,
        helperFactory,
        ShoppingListController;
    beforeEach(function() {

                                  /*   Trying to mock the Module again  */
                                  /*    after parent suite's inject() 
            module('myApp');      /*    This is what is triggering error  */
                                  /* REMOVE THIS MODULE MOCK FROM THIS SUITE */ 

        inject(function($rootScope, _helperFactory_, $controller) {

           $scope = $rootScope.$new();
           helperFactory = _helperFactory_;
           ShoppingListController = $controller('ShoppingListController', {
               $scope : $scope,
               helperFactory : helperFactory
           });
        });
    });
    it('should return 0 for 2 characters', function() {
        //Some Test
    });
  });
}); 

Hope this helps 希望这可以帮助

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM