in angular js while testing the controller got Unknown provider

I have the following controller :

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

Which dependent on the following service :

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

Tried to test the controller using the following 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() {

    });
});

But got the following error:

Error: Unknown provider: MainCtrlProvider <- MainCtrl.

Ps Tried the following post, didn't seem to help


The correct way to test controllers is to use $controller as such:

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

Detailed example:

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/77626.html

上一篇: AngularJS错误:未知提供者:sharedServiceProvider <

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