mercredi 27 mai 2015

$injector:moduleerr in angular unit test

I've been struggling for a day now just to start testing a specific control in an angular app. I went trough many questions here, tried many things and I still can't get it working so that is why I am asking you. Currently I have the following controller:

angular.module('selfServiceApp').controller('addUsersController', ['$scope', 'notificationService', 'resourceService', 'configService', '$timeout',
function ($scope, notificationService, resourceService, configService, $timeout) {

    $scope.email = { value: '' };
    $scope.users = [];
    $scope.canEdit = false;
    $scope.isProcessing = false;
    $scope.isUserSelected = false;
    $scope.userText = '';

    $scope.init = function(text) {
        $scope.userText = text;
    };
    ......
    $scope.removeUser = function (e) {
        if (!$scope.canEdit)
            return;

        var userId = parseInt(e.args);
        $scope.users = _.reject($scope.users, function (user) {
            return user.id == userId;
        });

        $scope.$emit("userRemoved", userId);
    };
}]); 

As you can see three services are injected + $timeout. I just want to perform an initialization test to check that everything is loaded as expected so I got the following jasmine test:

describe("Controller: addUsersController", function(){
describe("Testing on usersLoaded", function() {

    var $controller, $scope, $timeout, mockNotificationService, mockResourceService, mockConfigService;

    beforeEach(module("selfServiceApp", function ($provide){

        mockNotificationService = jasmine.createSpyObj("notificationService", ["error"]);
        //mockNotificationService.error.andReturn({something});

        mockResourceService = jasmine.createSpyObj("resourceService", ["getUserId"]);

        mockConfigService = {};

        $provide.value("notificationService", mockNotificationService);
        $provide.value("resourceService", mockResourceService);
        $provide.value("configService", mockConfigService);
    }));

    beforeEach(inject(function (_$controller_, $rootScope, _notificationService_, 
        _resourceService_, _configService_, _$timeout_){

        $scope = $rootScope.$new();
        mockNotificationService = _notificationService_;
        mockResourceService = _resourceService_;
        mockConfigService = _configService_;
        $timeout = _$timeout_;

        $controller = _$controller_("addUsersController", {
            $scope: $scope,
            notificationService : mockNotificationService,
            resourceService : mockResourceService,
            configService : mockConfigService,
            $timeout : $timeout
        });
    }));

    it("Controller should be initialized", function (){
        expect($scope.isProcessing).toBeDefined();
    });

});

});

When I try to run it I end up with :

Error: [$injector:moduleerr] ...... TypeError: 'undefined' is not an object (evaluating '$scope.isProcessing')

Any ideas why this is happening? Do I miss something fundamental in the angular unit testing? This is what I have included in the jasmine config file:

files: [
    'lib/angular.1.2.16.min.js',
    'lib/angular-mocks.js',
    'selfServiceApp.js',
    'controller/*.js',
    'selfServiceTests/testAddUsersController.js'
],

Aucun commentaire:

Enregistrer un commentaire