2

我创建这个来装饰 $log:

    window.fofr = window.fofr || {};
window.fofr.library = window.fofr.library || {};
window.fofr.library.logging = window.fofr.library.logging || {};

window.fofr.library.logging.errorLogViewerService = function () {
    var configure = function (angularJsModule) {
        angularJsModule.config(function ($provide) {
            $provide.decorator('$log', function ($delegate, $sniffer) {
                var _error = $delegate.error; // Saving original function
                var _log = $delegate.log;
                $delegate.logs = [];

                $delegate.error = function (msg) {
                    _error(msg);
                };

                $delegate.log = function (msg) {
                    _log(msg);

                    $delegate.logs.push(msg);
                };

                return $delegate;
            });
        });
    };

    return {
        configure: configure
    };
} ();

我用 qunit 创建了一个单元测试:

    module('Library - Logging - ErrorLogViewer', {
    setup: function () {
        this.app = angular.module('app', []);        
    }
});

test('Log - check the logs is filled with log', function () {
    window.fofr.library.logging.errorLogViewerService.configure(this.app);
    var injector = angular.injector(['app', 'ng']);

    injector.invoke(function ($log) {
        $log.log('test');
        equal($log.hasOwnProperty('logs'), true, 'The property logs must exists');
        equal($log.logs.length, 1, 'The logs must contain one log');
    });
});

但是它在配置中崩溃说它不知道 logProvider ???

4

1 回答 1

2

好的,我发现,$log 是在 ng 模块中定义的,在模块中的角度搜索由代码中设置的顺序定义: angular.injector(['app', 'ng']); 所以我设置了这个 angular.injector(['ng', 'app']); 现在它可以工作了!

于 2013-09-25T12:43:06.770 回答