injector already created. can not register a module

前端 未结 3 2021
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 15:11

I am a new bee to Angular JS and was trying to make something out of it in a proper TDD way, but while testing i am getting this error:

Injector alrea

相关标签:
3条回答
  • 2020-12-15 15:35

    Not sure that this is the cause, but your beforeEach should be like this:

    beforeEach(function() {
      inject(function($injector) {
        authorService = $injector.get('authorService');
      }
    });
    
    0 讨论(0)
  • 2020-12-15 15:40

    You're using the inject function wrong. As the documentation states, the inject function already instantiates a new instance of $injector. My guess is that by passing $injector as a argument to the inject function you are asking it to instantiate the $injector service twice.

    Just use inject to pass in the service you want to check. Underneath the covers, inject will use the $injector service it instantiates to grab services.

    You can fix this problem by changing the second beforeEach statement to:

    beforeEach(inject(function(_authorService_) {
        authorService = _authorService_;
    }));
    

    One other thing to note. The argument authorService passed to the inject function has been wrapped with '_' so it's name does not hide the variable created within the describe function. Thats also documented in the inject documentation.

    0 讨论(0)
  • 2020-12-15 15:43

    If you are mixing calls to module('someApp') and inject($someDependency) you will get this error.

    All your calls to module('someApp') must occur before your calls to inject($someDependency).

    0 讨论(0)
提交回复
热议问题