Take the 2-minute tour ×

I have the following controller:

  .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(inject(function(MainCtrl, Samples, _$httpBackend_) {


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


But got the following error:

Error: Unknown provider: MainCtrlProvider <- MainCtrl.

P.s Tried the following post, didn't seem to help

share|improve this question

1 Answer 1

up vote 13 down vote accepted

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(inject(function($controller, $rootScope, Samples) {

  scope = $rootScope.$new();
  service = Samples;
  //Create the controller with the new scope

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

share|improve this answer
The reason you can't load a controller as dependency is because the controllers are held inside a registry and don't have a provider. That's why you get the above error.. Only values / constants / factories / services can be loaded as dependencies. –  ShaiRez Jan 18 '13 at 15:26
@ShaiRez, thanks so much. I was messing with this for hours and then I read your comment for about the 3rd time and it clicked! Thanks again. –  ken Nov 19 '13 at 21:15

Your Answer


