0
我有一個錯誤處理模塊,它也進行一些日誌記錄。這裏是我的角的js模塊:使用angularjs/Intern/BDD測試錯誤
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可用於您的測試代碼,並且您認爲它在那裏嗎? – 2014-10-08 01:45:00