Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Karma-未知提供程序错误:menuFactoryProvider<;-菜单工厂_Javascript_Angularjs_Jasmine_Karma Jasmine_Angular Mock - Fatal编程技术网

Javascript Karma-未知提供程序错误:menuFactoryProvider<;-菜单工厂

Javascript Karma-未知提供程序错误:menuFactoryProvider<;-菜单工厂,javascript,angularjs,jasmine,karma-jasmine,angular-mock,Javascript,Angularjs,Jasmine,Karma Jasmine,Angular Mock,当尝试测试我的控制器时,Karma失败,出现了一系列错误,所有错误都以以下开头: Karma-错误:[$injector:unpr]未知提供程序:menuFactoryProvider 您在注入模块后意外关闭了descripe方法,这就是为什么它无法注入您的服务。现在可以了 describe('Controller: MenuController', function () { // load the controller's module beforeEach(mod

当尝试测试我的控制器时,Karma失败,出现了一系列错误,所有错误都以以下开头:

Karma-错误:[$injector:unpr]未知提供程序:menuFactoryProvider
您在注入模块后意外关闭了descripe方法,这就是为什么它无法注入您的服务。现在可以了

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();

      });
      ...
      });
 });

嗨,乔拉瓦。我尝试了您的配置,Karma仍然返回
错误:[$injector:unpr]未知提供程序:menuFactoryProvider您是否将此文件添加到Karma配置中?它添加了
'app/scripts/*.js'
我可以尝试显式添加controllers.js以确保。查看浏览器,它正在正确加载,您可能是指测试文件。添加了
'test/unit/***/.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;
                });
'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];
                };


        }])
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();

      });
      ...
      });
 });