Angular controller is not registering during unit testing

2.8k Views Asked by At

I'm attempting an Angular app with a Grails backend, and I'm unit testing the controller, simply to see if it exist, before I start working with it. Unfortunately, it is giving me the following error:

[$controller:ctrlreg] The controller with the name 'SecurityController' is not registered.

Here's my unit testing code:

import angular from 'angular';
import 'angular-mocks';
import worldManagerApp from '../../src/world-manager-app';
import security from '../../src/security/security';

const {inject, module} = angular.mock;

describe('SecurityController', () => {
    let $state;
    let vm;

    beforeEach(module(worldManagerApp));
    beforeEach(module(security));

    beforeEach(inject((_$state_, $controller) => {
        $state = _$state_;
        spyOn($state, 'go');

        vm = $controller('SecurityController', {
        });
    }));

    it('should be registered', () => {
        expect(vm).toBeDefined();
    });

});

Here is the controller code:

function SecurityController(){
    'ngInject';

    const vm = this;
    vm.security = "secure";
}

export default SecurityController;

And here is the module code, for good measure:

import angular from 'angular';
import uiRouter from 'angular-ui-router';

import SecurityController from './securityController'
import SignUp from './services/signUpService';
import AuthenticationService from './services/authService'

const security = angular.module('security', [
    uiRouter,
]).controller(SecurityController)
    .service(SignUp)
    .service(AuthenticationService);

export default security.name;

The security module is patched into my main app module, so I can provide that as well. I've read through a few resources regarding this, but nothing I've tried has been particularly helpful so far.

Thanks in advance!

1

There are 1 best solutions below

1
On BEST ANSWER
.controller(SecurityController)
.service(SignUp)
.service(AuthenticationService);

should be

.controller('SecurityController', SecurityController)
.service('SignUp', SignUp)
.service('AuthenticationService', AuthenticationService);

You need to provide the name ov the controller/service, and then its implementation.