0

This is my AngularJs code, I would like to write a unit test which should call variables and functions inside the controller function.

For example, I would like to expect whether the abcCode is toBeDefined();

please let me know how to load the module and to call the properties inside the controller function (abcCodesCntrl). your suggestions would be really helpful. thanks

function () {

angular.module('abcapp',[])
    .component('abcCodes', {
        templateUrl: 'app/abcCodes/abcCodes.html',
        controller: abcCodesCntrl
    })
    .config(function ($stateProvider) {
        $stateProvider
            .state('abcCodes', {
                parent: 'home',
                url: 'abcCodes',
                template: '<abc-codes flex layout="column"></abc-codes>',
                data: {
                    label: "abc Codes",
                    icon: 'business',
                    menu: true
                }
            });
    });

/** @ngInject */
function abcCodesCntrl(F $state, abcCodeSvc, $scope, $mdDialog) {
    var ctrl = this;


    ctrl.abcCodes = [];

    ctrl.loadingabcCodes = false;

} }

1 Answer 1

1
beforeEach(() => {
  angular.mock.module('abcapp');
});

it('pass', inject( $rootScope => {
  const scope = $rootScope.$new();
  const element = angular.element('<abc-codes/>');
  const template = $compile(element)(scope);
  const controller = template.controller('abcCodes');
  expect(controller.abcCodes).toBeDefined();
}))

if you can use es6 than controller become class

export class abcCodesCntrl{
  /** @ngInject */
  constructor( $state, abcCodeSvc, $scope, $mdDialog) {
    this.$state = $state;
    this.abcCodeSvc = abcCodeSvc;
    this.$scope = $scope;
    this.$mdDialog = $mdDialog;
    this.abcCodes = [];
    this.loadingabcCodes = false;
  }
}

not much improvement, but you could test controller separated from directive which mean you dont need create and initialize the element. Just test the functionality it self. Also soon as you add methods, its much easier to read.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your input. Here we can't change our Dev code. we have not used es6 . the above code didn't work for us. not able to expect or call the variable or function which is inside a controller.
describe('test', function () { var $compile,$rootScope; beforeEach(module('ui.router')); beforeEach(() => { angular.mock.module('abcapp'); }); beforeEach(inject(function($compile, $rootScope){ $compile = $compile; $rootScope = $rootScope; })); it('pass', function () { const element = angular.element('<abc-codes/>'); const template = $compile(element)($rootScope); const controller = template.controller('abcCodesCntrl'); expect(controller.abcCodes).toBeDefined(); }) })
Could you please suggest some solution for this, it would be really helpful for us to proceed further. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.