samedi 28 novembre 2015

Unit test Angular controller with Jasmine/Chutzpah

I am trying to write unit tests for a controller. However I am facing $injector:moduleerr when trying to run the tests.

Below is my controller code

(function () {
'use strict';

angular
    .module('MonitoringUI.Controllers')
    .controller('MonitoringController', ['$scope', '$filter', '$rootScope', '$modal', 'MonitoringService', '$sessionStorage', '$timeout', 'uiGridConstants', MonitoringController])
    .filter('unique', uniqueFilter);

// controller class definintion
function MonitoringController($scope, $filter, $rootScope, $modal, MonitoringService, $sessionStorage, $timeout, uiGridConstants) {

    $scope.ttypeList = [];
    $scope.inbound = false;
    $scope.outbound = false;
    $scope.multselectobj = [];
    $scope.advSearch = {};
    $scope.flowLabel = '';

    $scope.advSearch.status = 'All';
})();

And below is my Jasmine test js file

///<reference path="../../Scripts/jasmine.js"/>
///<reference path="../../Scripts/angularjs/angular.min.js"/>
///<reference path="../../Scripts/angularjs/angular-mocks.js"/>
///<reference path="../controllers/monitoring.controller.js"/>






describe('MonitoringController',function() {

var monitoringController,$scope;

beforeEach(angular.mock.module('MonitoringUI.Controllers'));
beforeEach(angular.mock.inject(function($rootScope, $controller) {
    $scope = $rootScope.$new();
    monitoringController = $controller('MonitoringController', {
        $scope: $scope
    });
}));
it('Variable should be false by default', function () {

    expect($scope.inbound).toBe(false);
});

})

When I'm trying to run the tests, I'm getting below error

$injector:modulerr Failed to instantiate module MonitoringUI.Controllers due to:
{1}

Do I need to mock all the dependencies of the controller and pass them?

Aucun commentaire:

Enregistrer un commentaire