Angular 模块仅在测试时执行两次

Angular module is executed twice only when testing

我的 angular 应用程序在 运行 正常时工作正常,但在我进行单元测试时似乎被执行了两次(使用 Karma 和 PhantomJS)

实例化如下:

bootstrap.js

require('./app.js');

var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;

if ( app ) {
    console.log('App running on a mobile device');
    document.addEventListener("deviceready", function(){
        angular.bootstrap(document, ['esme']);
    });
} else {
    console.log("App running in a browser");
    angular.element(document).ready(function() {
        angular.bootstrap(document, ['esme']);
    });
}

app.js

var esme = angular.module('esme', ['ui.router', 'ngAnimate', 'ngTouch', 'DataStore', 'angular-carousel', 'SessionParams', 'hmTouchEvents']);

esme.run(['$location', '$rootScope', '$state', 'NotificationManager', function($location, $rootScope, $state, NotificationManager) {
    console.log('run');

    NotificationManager.init();

    $rootScope.$on('$locationChangeSuccess', function (event, current, previous) {
        if($state.current.data)
            $rootScope.title = $state.current.data.title;
    });
}]);

Test.js

describe('TestController', function() {
    var $controller;
    var $rootScope;

    beforeEach(module('esme'));

    beforeEach(module(function($urlRouterProvider) {
        $urlRouterProvider.deferIntercept();
    }));

    beforeEach(inject(function(_$controller_, _$rootScope_){
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $controller = _$controller_;
        $rootScope = _$rootScope_.$new();
    }));

    describe('$scope.cheer', function() {
        var $scope, controller;

        beforeEach(function() {
            $scope = $rootScope;
            controller = $controller('Main', { $scope: $scope });
        });

        describe("#hello()", function(){
            it('ensures hello is correct', function() {
                $scope.test.should.equal('test');
            });
        });
    });
});

浏览器控制台的输出如下:

"App running in a browser"

"config"

"run"

但是单元测试的输出如下:

INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/

[ .. Karma / PhantomJS stuff .. ]

LOG: 'App running in a browser'

WARN [web-server]: 404: /views/Main/Login.html

LOG: 'config'

LOG:'run'

LOG: 'config'

LOG: 'run'

PhantomJS 1.9.8 (Windows 7): Executed 1 of 1 SUCCESS (0.007 secs / 0.001 secs)

WARN [web-server]: 404: /views/Main/Login.html

[17:16:14] Finished 'test:unit' after 5.31 s

我仍在学习如何模拟 angular 应用程序,因此我坚信我在测试文件中犯了一些明显的错误。你能帮我找到他们并解释为什么会发生这种行为吗?

我现在唯一的线索可能来自 Angular's doc我应该只为 运行 应用程序声明一个模块吗?:

Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the service have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

感谢您的帮助!

嗯,一夜好眠,答案直截了当...

因为我在进行手动引导,所以在我的 karma.conf 文件中,我简单地排除了完成引导的文件。

Karma.conf

files: [
    'lib/angular-mocks/angular-mocks.js',
    'src/js/**/*.js',
    'test/test.js'
],

// list of files to exclude
exclude: [
    'src/js/bootstrap.js'
],