【问题标题】:Testing errors with angularjs/Intern/BDD使用 angularjs/Intern/BDD 测试错误
【发布时间】:2014-10-07 17:01:52
【问题描述】:

我有一个错误处理模块,它也进行一些日志记录。这是我的 Angular js 模块:

angular.module('error_handling', [])
.service('default_logger', function () {
    // default logging
    this.logging_mode = 'debug';

    function InvalidInputException(message) {
        this.name = 'InvalidInputException';
        this.message = message;
    }

    InvalidInputException.prototype = new Error();
    InvalidInputException.prototype.constructor = InvalidInputException;

    this.set_logging_mode = function(mode){
        if (mode !== 'log' && mode !== 'debug' && mode !== 'info' && mode !== 'warn' && mode !== 'error' && mode !== 'all' && mode !== 'off'){
            throw new InvalidInputException('Invalid logging mode.');
        }

        this.logging_mode = mode.toLowerCase();
    };

    this.log = function (msg) {
        check_mode('log', this.logging_mode) && console.trace(msg);
    };

    this.debug = function (msg) {
        check_mode('debug', this.logging_mode) && console.debug(msg);
    };

    this.info = function (msg) {
        check_mode('info', this.logging_mode) && console.info(msg);
    };

    this.warn = function (msg) {
        check_mode('warn', this.logging_mode) && console.warn(msg);
    };

    this.error = function (msg) {
        check_mode('error', this.logging_mode) && console.error(msg);
    };

    function check_mode(action, logging_mode){
        if (logging_mode === 'debug' || logging_mode === 'all'){
            return true;
        }
        else if(logging_mode === action){
            return true;
        }
        else if(logging_mode === 'off'){
            return false;
        }
        else{
            return false;
        }
    };
})
.factory('delegated_default_logger', ['default_logger', function(default_logger) {
    return function($delegate) {
        //TODO: actually use the $delegate variable? (which should equal angular's default $log service)
        return angular.extend({}, default_logger);
    };
}])
.config(function($provide) {
    $provide.decorator('$exceptionHandler', ['$log', '$delegate',
      function($log, $delegate) {
        return function(exception, cause) {
          $log.debug('Default exception handler.');
          $delegate(exception, cause);
        };
      }
    ]);
});

这是我的测试文件:

define([
    'intern!bdd',
    'intern/chai!expect',
    //'intern/order!node_modules/intern/chai',
    // 'intern/order!node_modules/chai/lib/chai',
    // 'intern/order!node_modules/sinon/lib/sinon',
    // 'intern/order!node_modules/sinon-chai/lib/sinon-chai',
    'intern/order!vendor/src/sinonjs-built/lib/sinon',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/spy',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/call',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/behavior',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/stub',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/mock',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/collection',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/assert',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/sandbox',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/test',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/test_case',
    'intern/order!vendor/src/sinonjs-built/lib/sinon/match',

    'intern/order!vendor/src/angular/angular',
    'intern/order!vendor/src/angular-mocks/angular-mocks',
    'intern/order!src/common/modules/error_handling/error_handling',
    'intern/order!src/app'
], function (bdd, expect) {
    //
    with (bdd) {

        describe('Error handler module', function () {
            var test, scope, ctrl, error_handler, log;

            function inject (fn) {
                return function() {
                    angular.injector(['ng', 'ngMock', 'ngNomi']).invoke(fn);
                }
            }

            beforeEach(inject(function($log){
                log = $log;
            }));

            it('should be an object', function(){
                expect(log).to.be.an('object');
            });

            it('should default to debug logging mode', function() {
                expect(log.logging_mode).to.equal('debug');
            });

            it('should call console.debug with the string test and default logging mode', function(){
                var spy = sinon.spy(console, 'debug');

                log.debug('test');
                expect(console.debug.calledOnce).to.be.true;
                expect(console.debug.calledWith('test')).to.be.true;

                console.debug.restore();
            });

            it('should be able to set logging mode', function(){
                log.set_logging_mode('off');

                expect(log.logging_mode).to.equal('off');
            });

            it('should throw an error on invalid logging mode', function(){
                expect(log.set_logging_mode('bad_mode')).to.throw(InvalidInputException);
            });

        });
    }
});

我所有的测试都通过了,除了最后一个,它给了我这个输出:

>> 1/9 tests failed
Warning: FAIL: main - Error handler module - should throw an error on invalid logging mode (0ms)
InvalidInputException: Invalid logging mode.
  at </Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/src/common/modules/error_handling/error_handling.js:16>
  at </Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/src/common/modules/error_handling/error_handling.test.js:67>
  at <__intern/lib/Test.js:169>
  at <__intern/lib/Suite.js:237>
  at <__intern/node_modules/dojo/Deferred.js:37>
  at <__intern/node_modules/dojo/Deferred.js:258>
  at runTest  <__intern/lib/Suite.js:241>
  at <__intern/lib/Suite.js:249>
1/5 tests failed
1/9 tests failed
1/9 tests failed Use --force to continue.

它表现得就像遇到错误一样,这很好,但它没有被测试捕获(即通过)。为什么会这样?另外,我是否以任何有意义的方式构建 error_handling 模块?我应该将错误类放在那个位置的那个文件中,还是其他地方?

【问题讨论】:

  • 您确定 InvalidInputException 可用于您的测试代码,并且您认为它存在吗?

标签: javascript angularjs intern


【解决方案1】:

在检查是否抛出异常时,需要传递expect一个函数来调用,比如:

expect(function () { log.set_logging_mode('bad_mode') }).to.throw(InvalidInputException);

当您将函数调用(如log.set_logging_mode('bad_mode'))作为参数传递给expect 时,将在调用expect 之前评估该调用(并引发异常)。

【讨论】:

    猜你喜欢
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多