lundi 29 août 2016

unit testing controller with stateParams

I want to test the functional of controller.

angular.module('myApp').controller('appCtrl', function ($scope, appCache, $stateParams) {
    var id;

    if ($stateParams.type === 'admin') {
        id = 'admin';
    } else {
        id= appCache.getId();
    }
});

I have issue with second test, with first condition in controller, id is not set to 'admin', it is still undefined.

describe('appCtrl', function () {
'use strict';

var ctrl,
    stateParams,
    id,
    scope;

beforeEach(module('com.bmc.arsys.rx.standardlib.administration-settings'));

beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    stateParams = {
        type: 'admin'
    };
    ctrl = $controller('appCtrl', {$scope: scope, $stateParams: stateParams});

}));

it('should exist', function () {
    expect(ctrl).toBeDefined();
});

it('should set admin to id if type for stateParams is admin', function () {
    scope.$digest();
    expect(id).toBe('admin'); // not passed, id is still undefined 
});
});

What is incorrect in my second test?

Aucun commentaire:

Enregistrer un commentaire