在角js测试时,控制器得到未知提供者

我有以下控制器

angular.module('samples.controllers',[])
  .controller('MainCtrl', ['$scope', 'Samples', function($scope, Samples){
  //Controller code
}

依赖于以下服务

angular.module('samples.services', []).
    factory('Samples', function($http){
    // Service code
}

尝试使用以下代码测试控制器

describe('Main Controller', function() {
  var service, controller, $httpBackend;

  beforeEach(module('samples.controllers'));
  beforeEach(module('samples.services'));
  beforeEach(inject(function(MainCtrl, Samples, _$httpBackend_) {

  }));

    it('Should fight evil', function() {

    });
});

但有以下错误:

Error: Unknown provider: MainCtrlProvider <- MainCtrl.

Ps试过以下帖子,似乎没有帮助


测试控制器的正确方法是使用$控制器:

ctrl = $controller('MainCtrl', {$scope: scope, Samples: service});

详细示例:

describe('Main Controller', function() {
  var ctrl, scope, service;

  beforeEach(module('samples'));

  beforeEach(inject(function($controller, $rootScope, Samples) {
    scope = $rootScope.$new();
    service = Samples;

    //Create the controller with the new scope
    ctrl = $controller('MainCtrl', {
      $scope: scope,
      Samples: service
    });
  }));

  it('Should call get samples on initialization', function() {

  });
});
链接地址: http://www.djcxy.com/p/77625.html

上一篇: in angular js while testing the controller got Unknown provider

下一篇: Unknown provider error when deploying Rails/AngularJS app to Heroku