【问题标题】:Karma - Unknown provider error: menuFactoryProvider <- menuFactoryKarma - 未知的提供程序错误:menuFactoryProvider <- menuFactory
【发布时间】:2016-07-28 00:59:17
【问题描述】:

在尝试测试我的控制器时,Karma 失败并出现一串错误,所有错误均以:

Karma - 错误:[$injector:unpr] 未知提供者:menuFactoryProvider

似乎 menuFactory(现在实际上是一项服务)没有正确注入,但我不知道为什么。为了清楚起见,此处显示了业力输出:

这是我的 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();

  });
  ...
  });

摘自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;
                });

摘自 services.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];
                };


        }])

【问题讨论】:

    标签: javascript angularjs jasmine karma-jasmine angular-mock


    【解决方案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();
    
          });
          ...
          });
     });
    

    【讨论】:

    • 嗨乔拉瓦。我试过你的配置,业力仍然返回Error: [$injector:unpr] Unknown provider: menuFactoryProvider &lt;- menuFactory
    • 您是否将此文件添加到 karma-config 中?
    • 它是用'app/scripts/*.js'添加的,我可以尝试显式添加controllers.js来确定。
    • 查看浏览器是否正常加载
    • 哦,你可能指的是测试文件。这是用'test/unit/**/*.js'添加的
    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多