-2

Can anyone help me find a way to use both beforeEach(module()) and beforeEach(inject()) in the main describe() of my unit test?

When I include both

beforeEach(module('ngRoute'));

and

beforeEach(inject(function () {}));

in the main describe() of my test file, I get this error. But, If I remove the beforeEach(inject () {})) code, then ngRoute is properly loaded by karma.

I have checked many other posts related to this issue. Such as:

As suggested by each of the above questions, I have made sure that angular-routes.js is included in my index.html and I have also made sure that 'ngRoute' is a dependency of my app.

test file :

define(['ngMock', 'controllers/home/main-home-ctrl'], function () {
    'use strict';

    describe.only('Controller: MainHomeController', function () {
        beforeEach(module('ngRoute'));
        beforeEach(inject(function () {}));

        afterEach(function() {

        });

        describe('', function () {
            it('should ', function () {

            });
        });

    });
});

karma.config.js :

files: [
            {pattern : 'www/js/bootstrap.js', included : false },
            {pattern : 'www/js/lib/*.js', included : false },
            {pattern : 'www/js/controllers/*.js', included : false },
            {pattern : 'www/js/controllers/**/*.js', included : false },
            {pattern : 'www/js/directives/*.js', included : false },
            {pattern : 'www/js/directives/**/*.js', included : false },
            {pattern : 'www/js/factories/*.js', included : false },
            {pattern : 'www/js/factories/**/*.js', included : false },
            {pattern : 'www/js/filters/*.js', included : false },
            {pattern : 'www/js/filters/**/*.js', included : false },
            {pattern : 'www/js/services/*.js', included : false },
            {pattern : 'www/js/services/**/*.js', included : false },
            {pattern : 'www-test/lib/*.js', included : false },
            {pattern : 'www-test/spec/**/*.js', included : false },
            {pattern : 'www-test/spec/**/**/*.js', included : false },
            'www-test/main-test.js',
            'www/js/lib/angular-route.js'
        ],

angular-routes.js is located at 'www/js/lib/angular-routes.js' and therefore should be included when karma runs my unit tests.

app.js :

    define([
        'angular',
        'angular-route',
        'controllers/index',
        'directives/index',
        'factories/index',
        'filters/index',
        'services/index'
    ], function (ng) {
        'use strict';
        return ng.module('app', ['ngRoute', 'app.controllers', 'app.directives', 'app.factories', 'app.filters', 'app.services']);
    });
Community
  • 1
  • 1
Riley P
  • 73
  • 1
  • 6
  • If you never call `inject()`, you never push angular into the _"run"_ phase and so the module configuration code is never actually run. That's why there's no error when you remove `inject()`. – hon2a Mar 20 '15 at 17:48

1 Answers1

-1

So, I found my rookie mistake. Angular-route is being included in my production and dev code, but not in my test code because I only added the file to my production and dev folders.

Riley P
  • 73
  • 1
  • 6