injector already created. can not register a module

前端 未结 3 2020
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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.

提交回复
热议问题