Getting Unknown Provider error when injecting a Service into an Angular unit test

后端 未结 4 1621
难免孤独
难免孤独 2021-02-06 23:01

I\'m fairly new to Angular and have reviewed all the similarly related questions on Stack Overflow but none have helped me. I believe I have everything set up correctly but am s

4条回答
  •  抹茶落季
    2021-02-06 23:45

    While this question is fairly old i lost significant time solving a problem similar to this one, i.e.:

    Error: Unknown provider: SomeServiceProvider <- SomeService
    

    Hence, i'm leaving here another possible cause to this issue. Hopefully, it would helpful to someone.

    In my case, i had in my project two modules with the exactly same name but with different dependencies being created, i.e., two different .js files with:

    angular.module('moduleName', [dependencies]))
    

    From angular documentation:

    Passing one argument retrieves an existing angular.Module, whereas passing more than one argument creates a new angular.Module

    Conclusion: It turns out that what was being injected in the test was the module with the wrong dependencies. Removing the second argument from the module that was erroneously being created solved the problem.

提交回复
热议问题