2016-07-27 19 views
1

Beim Versuch, meinen Controller zu testen, Karma mit einer Reihe von Fehlern nicht alle beginnend mit:Karma - Unbekannt Provider Fehler: menuFactoryProvider <- menuFactory

Karma - Error: [$injector:unpr] Unknown provider: menuFactoryProvider <- menuFactory

Es scheint menuFactory (die nun tatsächlich ein Dienst) isn 't richtig injiziert, aber ich kann nicht herausfinden, warum. Karma Ausgabe hier aus Gründen der Klarheit gezeigt:

enter image description here

Hier ist meine menucontroller-test.js:

describe('Controller: MenuController', function() { 

    // load the controller's module 
    beforeEach(module('confusionApp')); 

    var MenuController, scope, $httpBackend; 

}); 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function ($controller, _$httpBackend_, $rootScope, menuFactory) { 

      // place here mocked dependencies 
     $httpBackend = _$httpBackend_; 

     $httpBackend.expectGET("http://localhost:3000/dishes").respond([ 
     { 
     "id": 0, 
     ... 
     }, 
     { 
     "id": 1, 
     ... 
     } 
     ]); 

    scope = $rootScope.$new(); 
    MenuController = $controller('MenuController', { 
     $scope: scope, menuFactory: menuFactory 
    }); 
      $httpBackend.flush(); 

    })); 

    it('should have showDetails as false', function() { 

    expect(scope.showDetails).toBeFalsy(); 

    }); 
    ... 
    }); 

Auszug aus controllers.js

'use strict'; 

angular.module('confusionApp') 

     .controller('MenuController', ['$scope', 'menuFactory', function($scope, menuFactory) { 

      $scope.tab = 1; 
      $scope.filtText = ''; 
      $scope.showDetails = false; 
      $scope.showMenu = false; 
      $scope.message = "Loading ..."; 

      menuFactory.getDishes().query(
       function(response) { 
        $scope.dishes = response; 
        $scope.showMenu = true; 
       }, 
       function(response) { 
        $scope.message = "Error: "+response.status + " " + response.statusText; 
       }); 

Auszug aus services.js (Anmerkung wieder menuFactory ist eigentlich ein Service, keine Fabrik)

'use strict'; 

angular.module('confusionApp') 
     .constant("baseURL", "http://localhost:3000/") 
     .service('menuFactory', ['$resource', 'baseURL', function($resource, baseURL) { 

      var promotions = [ 
       { 
          _id:0, 
          name:'Weekend Grand Buffet', 
          image: 'images/buffet.png', 
          label:'New', 
          price:'19.99', 
          description:'Featuring mouthwatering combinations with a choice of five different salads, six enticing appetizers, six main entrees and five choicest desserts. Free flowing bubbly and soft drinks. All for just $19.99 per person ', 
       } 

      ]; 

       this.getDishes = function(){ 
             return $resource(baseURL+"dishes/:id",null, {'update':{method:'PUT' }}); 
            }; 

       // implement a function named getPromotion 
       // that returns a selected promotion. 
       this.getPromotion = function(index) { 
          return promotions[index]; 
       }; 


     }]) 

Antwort

1

Sie hatten die Methode describe nach dem Injizieren des Moduls versehentlich geschlossen, weshalb es Ihren Service nicht injizieren konnte. Es funktioniert jetzt!

describe('Controller: MenuController', function() { 

     // load the controller's module 
     beforeEach(module('confusionApp')); 

     var MenuController, scope, $httpBackend,menuFactory;  

     // Initialize the controller and a mock scope 
     beforeEach(inject(function ($injector,$controller, _$httpBackend_, $rootScope, _menuFactory_) { 

       // place here mocked dependencies 
      $httpBackend = _$httpBackend_; 
      menuFactory = $injector.get('menuFactory');     
      $httpBackend.expectGET("http://localhost:3000/dishes").respond([ 
      { 
      "id": 0, 
      ... 
      }, 
      { 
      "id": 1, 
      ... 
      } 
      ]); 

     scope = $rootScope.$new(); 
     MenuController = $controller('MenuController', { 
      $scope: scope, menuFactory: menuFactory 
     }); 
       $httpBackend.flush(); 

     })); 

     it('should have showDetails as false', function() { 

     expect(scope.showDetails).toBeFalsy(); 

     }); 
     ... 
     }); 
}); 
+0

Hallo Jorawar. Ich habe versucht, Ihre Konfig und Karma gibt immer noch 'Fehler: [$ injector: unpr] Unbekannter Anbieter: menuFactoryProvider <- menuFactory ' – CryptoPiggy

+0

fügen Sie diese Datei zu Karma-Config? –

+0

Es ist hinzugefügt mit ''app/scripts/*. Js'' Ich kann versuchen, explizit controller.js hinzufügen, um sicher zu sein. – CryptoPiggy