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

时间:2016-02-01 13:54:11

标签: angularjs unit-testing karma-jasmine

我是单元测试的新手,我在运行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 个答案:

答案 0 :(得分:0)

有趣的是,Angular无法确定class B: A = 1 def __init__(self): self.b = 2 class C: def __init__(self, a): self.a = a self.b = 3 b = B() c = B(a=b.A) $webSocketBackend.mock。我的 guess 是你的一个依赖项未能被注入,并且当Angular使用注入的依赖项时现在导致问题。

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

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

  

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