Wednesday 15 January 2014

javascript - Testing errors with angularjs/Intern/BDD -



javascript - Testing errors with angularjs/Intern/BDD -

i have error handling module logging. here angular js module:

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'){ homecoming true; } else if(logging_mode === action){ homecoming true; } else if(logging_mode === 'off'){ homecoming false; } else{ homecoming false; } }; }) .factory('delegated_default_logger', ['default_logger', function(default_logger) { homecoming function($delegate) { //todo: utilize $delegate variable? (which should equal angular's default $log service) homecoming angular.extend({}, default_logger); }; }]) .config(function($provide) { $provide.decorator('$exceptionhandler', ['$log', '$delegate', function($log, $delegate) { homecoming function(exception, cause) { $log.debug('default exception handler.'); $delegate(exception, cause); }; } ]); });

and here test file:

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) { // (bdd) { describe('error handler module', function () { var test, scope, ctrl, error_handler, log; function inject (fn) { homecoming function() { angular.injector(['ng', 'ngmock', 'ngnomi']).invoke(fn); } } beforeeach(inject(function($log){ log = $log; })); it('should object', function(){ expect(log).to.be.an('object'); }); it('should default debug logging mode', function() { expect(log.logging_mode).to.equal('debug'); }); it('should phone call console.debug string test , 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 able set logging mode', function(){ log.set_logging_mode('off'); expect(log.logging_mode).to.equal('off'); }); it('should throw error on invalid logging mode', function(){ expect(log.set_logging_mode('bad_mode')).to.throw(invalidinputexception); }); }); } });

all tests pass except lastly one, gives me output:

>> 1/9 tests failed warning: fail: main - error handler module - should throw error on invalid logging mode (0ms) invalidinputexception: invalid logging mode. @ </users/evanvandegriff/documents/work/nomi_v2/nomi_v2/web/src/common/modules/error_handling/error_handling.js:16> @ </users/evanvandegriff/documents/work/nomi_v2/nomi_v2/web/src/common/modules/error_handling/error_handling.test.js:67> @ <__intern/lib/test.js:169> @ <__intern/lib/suite.js:237> @ <__intern/node_modules/dojo/deferred.js:37> @ <__intern/node_modules/dojo/deferred.js:258> @ runtest <__intern/lib/suite.js:241> @ <__intern/lib/suite.js:249> 1/5 tests failed 1/9 tests failed 1/9 tests failed utilize --force continue.

it's acting encountering error, fine, not getting caught test (i.e. passing). why happening? also, structuring error_handling module in way makes sense? should putting error classes in file in spot, or somewhere else?

when you're checking whether exception thrown, need pass expect function call, like:

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

when pass function call, log.set_logging_mode('bad_mode'), parameter expect, phone call evaluated (and exception thrown) before expect called.

javascript angularjs intern

No comments:

Post a Comment