Skip to content

Instantly share code, notes, and snippets.

@climboid
Created March 29, 2015 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save climboid/71c41285e040bc57b47f to your computer and use it in GitHub Desktop.
Save climboid/71c41285e040bc57b47f to your computer and use it in GitHub Desktop.
Unit test failing
'use strict';
describe('Offers : controller', function(){
beforeEach(module('client'));
var $scope,
$rootScope,
controller,
genericService;
beforeEach(inject(function($injector) {
$rootScope = $injector.get('$rootScope');
genericService = $injector.get('genericService');
$scope = $rootScope.$new();
var $controller = $injector.get('$controller');
var createController = function() {
return $controller('OffersCtrl', {
$scope: $scope,
genericService : genericService
});
};
controller = createController();
}));
it('should have a fixed slider value', function () {
expect($scope.sliderValue).toEqual(4600);
});
});
//
// The error I get is
// Error: [$injector:unpr] Unknown provider: genericServiceProvider <- genericService
//
@alexrothenberg
Copy link

@climboid a few thoughts...

  1. instead of injecting the $injector then manually looking up the service and rootscope you can inject those directly in the beforeEach beforeEach(inject(function($rootScope, $controller, genericService) {..

  2. If there a service called genericService in your app called client what you have should work. Is it maybe called GenericService?

  3. You don't need to inject genericService at all. Since you are passing what angular provides you directly into the controller just omit that line and angular will do the right thing.

    var createController = function() {
      return $controller('OffersCtrl', {
        $scope: $scope
      });
    };
    
  4. Lastly you don't need to return anything in your createController function. The controller is created with the scope as a side-effect and you can now read and write on the scope to test the controller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment