2016-07-27 4 views
1

私のコントローラをテストしようとすると、カルマがすべてで始まるエラーの文字列で失敗します。カルマ - 不明プロバイダエラー:menuFactoryProvider < - menuFactory

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

それは(今実際にサービスである)menuFactoryはISNようです適切に注入されていないが、私は理由を理解できない。わかりやすくするためにここに示されているカルマ出力:services.jsからcontrollers.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(); 

    }); 
    ... 
    }); 

抜粋

'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; 
       }); 

抜粋(注:ここでは

enter image description here

は私のmenucontroller-test.jsです再びmenuFactoryは実際にはサービスではなく、工場ではありません)

'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]; 
       }; 


     }]) 

答えて

1

モジュールを注入した後、あなたのサービスを注入できなかったため、あなたは誤ってdescribeメソッドを終了しました。それは今働きます!

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

こんにちは。私はあなたの設定を試して、Karmaはまだ 'Error:[$ injector:unpr] Unknown provider:menuFactoryProvider < - menuFactory ' – CryptoPiggy

+0

をこのkarma-configに追加していますか? –

+0

これは ''app/scripts/*。js''で追加されました。私は、controllers.jsを明示的に追加してみることができます。 – CryptoPiggy

関連する問題