错误:[$injector:unpr] 未知提供者:$routeProvider,无法实例化模块 myApp,原因是

Posted

技术标签:

【中文标题】错误:[$injector:unpr] 未知提供者:$routeProvider,无法实例化模块 myApp,原因是【英文标题】:Error: [$injector:unpr] Unknown provider: $routeProvider, Failed to instantiate module myApp due to 【发布时间】:2016-05-10 00:10:41 【问题描述】:

我对单元测试很陌生,在运行 Grunt Test 时遇到了这个非常奇怪的错误。

TypeError: 'undefined' is not an object (evaluating '$websocketBackend.mock')


Error: [$injector:modulerr] Failed to instantiate module myApp due to:

Error: [$injector:unpr] Unknown provider: $routeProvider

spec.js

    describe('test d3mapping websocket', function () 
        var testScope, $controller, $websocketBackend;
        //
        //beforeEach(function() 
        //    module('myApp');
        //    module('ngWebSocket', 'ngWebSocketMock');
        //    inject(function(_$controller_, $rootScope, _$websocketBackend_) 
        //        $controller = _$controller_;
        //        testScope = $rootScope.$new();
        //
        //        $websocketBackend = _$websocketBackend_;
        //
        //    );
        //);

        describe('response', function() 

            beforeEach(function() 
                module('myApp');
                module('ngWebSocket', 'ngWebSocketMock');
                inject(function(_$controller_, $rootScope, _$websocketBackend_) 
                    $controller = _$controller_;
                    testScope = $rootScope.$new();

                    $websocketBackend = _$websocketBackend_;
                );
                $controller('MainCtrl', 
                    $scope: testScope
                );
            );

            it('should send/receive json',
                function() 
                    $websocketBackend.mock();
//server url omitted
                    $websocketBackend.expectConnect("ws://server");
                    $websocketBackend.expectSend(JSON.stringify('"nodes":["name":"A6EBL","group":2,"size":5,"name":"GBYGC","group":2,"size":5,"name":"KORD","group":1,"size":10,"name":"EGLL","group":1,"size":10,"name":"DNMM","group":1,"size":10],"links":["source":2,"target":1,"value":1,"source":3,"target":1,"value":1,"source":3,"target":1,"value":1,"source":2,"target":1,"value":1,"source":4,"target":1,"value":1,"source":3,"target":1,"value":1]'));
                
            );
        );
    );

    // test svg


    describe('Test D3.js with jasmine ', function () 
        var testScope;

        beforeEach(function () 

            module('myApp');
            inject(function ($controller, $rootScope) 
                testScope = $rootScope.$new();
                $controller('MainCtrl', 
                    $scope: testScope
                );
            );
        );



        describe('the svg', function () 
            it('should be created', function () 
                expect(getSvg()).not.toBeNull();
            );
        );

        function getSvg() 
            return document.getElementsByTagName('svg');
        

    );

任何建议都会有所帮助

【问题讨论】:

【参考方案1】:

有趣的是,Angular 无法确定$webSocketBackend.mock$routeProvider。我的猜测是您的依赖项之一未能注入,现在当 Angular 使用注入的依赖项时会导致问题。

只是猜测 - 但是,参考类似的 Stack Overflow 问题 Failed to instantiate module [$injector:unpr] Unknown provider: $routeProvider

您的控制器是否可以同时访问$routeProvider$webSocketBackend

ngRoute 模块不再是核心 angular.js 文件的一部分。如果 您将继续使用 $routeProvider 那么您现在需要 在您的 html 中包含 angular-route.js:

Scotty.NET

【讨论】:

以上是关于错误:[$injector:unpr] 未知提供者:$routeProvider,无法实例化模块 myApp,原因是的主要内容,如果未能解决你的问题,请参考以下文章

错误:[$injector:unpr] 未知提供者:$routeProvider,无法实例化模块 myApp,原因是

错误:[$injector:unpr] 未知提供者:$localStorageProvider <- $localStorage <- login

未捕获的错误:[$injector:unpr] 未知提供程序:$cordovaPushV5Provider <- $cordovaPushV5

未知提供者:$routeParamsProvider <- $routeParams

正则表达式测试作为属性的指令实现错误 - 验证(打字稿+角度)

不要在角度模态中保存范围更改