首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用angularjs/Intern/BDD测试错误

使用angularjs/Intern/BDD测试错误
EN

Stack Overflow用户
提问于 2014-10-08 01:01:53
回答 1查看 386关注 0票数 0

我有一个错误处理模块,它也可以做一些日志记录。这是我的angular js模块:

代码语言:javascript
复制
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);
        };
      }
    ]);
});

下面是我的测试文件:

代码语言:javascript
复制
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);
            });

        });
    }
});

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

代码语言:javascript
复制
>> 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模块?我应该将错误类放在文件中的那个位置,还是放在其他地方?

EN

回答 1

Stack Overflow用户

发布于 2014-11-03 07:49:41

在检查是否抛出异常时,需要向expect传递一个要调用的函数,如下所示:

代码语言:javascript
复制
expect(function () { log.set_logging_mode('bad_mode') }).to.throw(InvalidInputException);

当您将函数调用(如log.set_logging_mode('bad_mode') )作为参数传递给expect时,将在调用expect之前对该调用进行计算(并抛出异常)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26241335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档