jeudi 24 décembre 2015

Jasmine failing to instantiate Angular module?

Okay, so Im trying to implement unit tests for an AngularJS project using Jasmine 2.2.0 - and I am not able to get a basic example test working for a controller. Any help is appreciated.

Here is the code for the controller (the simplest one I have):

angular.module('app.alerts')
    .controller('AlertCtrl', AlertCtrl);

AlertCtrl.$inject = ['$scope', 'AlertService'];

function AlertCtrl($scope, AlertService) {

    var vm = this;
    vm.closeAlert = function(index) {
        AlertService.closeAlert(index);
    }

    vm.getAlertList = function() {
        return AlertService.getAlertList();
    }
}

And here is the spec file I am trying to run:

describe('myApp', function() {
    describe('controller', function() {

        var scope, controller;
        var mockAlertService = {      // simple mock service
            closeAlert: function(e) {
                console.log('close');
            },
            getAlertList: function() {
                return [];
            }
        }    

        beforeEach(function() {
            angular.mock.module('app.alert');
        });

        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            controller = $controller('AlertCtrl as alertctrl', {
                $scope: scope,
                AlertService: mockAlertService
            });
        }));

        it('should work: ', function() {
            expect(true).toBe(true);
        });
    });
});

When I run the test I get the following error message:

Error: [$inject:modulerr] Failed to instantiate module app.alert due to:
[$injector:nomod] Module 'app.alert' is not available! You either misspelled
the module name or forgot to load it.

It seems that the injector doesn't know about the app.alert module, which means its not being properly mocked? Im including the angular-mocks.js file in my SpecRunner.html file. Can anybody see what Im doing wrong?

Aucun commentaire:

Enregistrer un commentaire