Question

I'm trying to do unit testing with angular js but it's not easy.

I want to proceed a simple trivial test like this :

describe('Edition Controllers', function(){
  beforeEach(module('DOPCjsControllers.controller'));


it('should check a simple test', inject(function($controller) {
  var scope = {};
  var editionCtrl = $controller('EditionController', {$scope : scope} );
  expect(scope.test).toBe("lol");

   }));
});

My controller's begining :

'use strict';
/* Controllers */

var DOPCjsControllers = angular.module('DOPCjsControllers', []);

/**
*EditionController : Controlleur de gestion de la partie edition du logiciel.
*/
DOPCjsControllers.controller('EditionController', ['$scope', '$http', '$routeParams', '$filter', '$modal','$fileUploader', 'AUTH_EVENTS','NotificationFactory', 'NOTIF_STATUTS','Session','AuthService','$location',
function($scope, $http, $routeParams, $filter, $modal, $fileUploader, AUTH_EVENTS, NotificationFactory, NOTIF_STATUTS, Session, AuthService, $location){
    $scope.test = "lol";
    $scope.$on(AUTH_EVENTS.notAuthenticated, function(event){
        //alert("Connectez vous d'abord");
        console.log("notAuthenticated");

    }); etc ...

When i execute the test i see : Firefox 29.0.0 (Ubuntu): Executed 1 of 1 (1 FAILED) ERROR (0.645 secs / 0.011 secs) INFO [watcher]: Changed file "/home/lionnel/angular-seed/test/unit/controllersSpec.js". Firefox 29.0.0 (Ubuntu) Edition Controllers should check a simple test FAILED minErr/<@/home/lionnel/angular-seed/bower_components/angular/angular.js:78 loadModules/<@/home/lionnel/angular-seed/bower_components/angular/angular.js:3810 forEach@/home/lionnel/angular-seed/bower_components/angular/angular.js:323 loadModules@/home/lionnel/angular-seed/bower_components/angular/angular.js:3775 createInjector@/home/lionnel/angular-seed/bower_components/angular/angular.js:3715 workFn@/home/lionnel/angular-seed/bower_components/angular-mocks/angular-mocks.js:2142

do you know what's the trick ?

Thanks for your help

Unfortunately when i write :

describe('Edition Controllers', function(){
beforeEach(module('DOPCjsControllers'));


    it('should check a simple test', inject(function($controller) {
      var scope = {};
      var editionCtrl = $controller('EditionController', {$scope : scope} );
      expect(scope.test).toBe("lol");

    }));
});

i have this error message : Error: [$injector:unpr] Unknown provider: $routeParamsProvider <- $routeParams http://errors.angularjs.org/1.2.16/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams

Was it helpful?

Solution

Your test tries to load a module named "DOPCjsControllers.controller". You don't have any such module. The module you're defining is called "DOPCjsControllers":

var DOPCjsControllers = angular.module('DOPCjsControllers', []);
        That's the name of the module ----^
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top