From 50c2902b09237eb23bb028c3ffe1230d7b2cf8f9 Mon Sep 17 00:00:00 2001 From: David Souther Date: Tue, 23 Dec 2014 10:10:39 -0500 Subject: [PATCH 1/2] fix($exceptionHandlerProvider): call `inject()` to run tests In the current angular-mocksSpec, the tests for $exceptionHandlerProvider call `module` to run tests on `$exceptionHandlerProvider.mode()`, but do not call `inject()` to pump the module definitions. --- test/ngMock/angular-mocksSpec.js | 35 +++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index d9773b42904b..4b09cbd84719 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -592,22 +592,29 @@ describe('ngMock', function() { })); - it('should log exceptions', module(function($exceptionHandlerProvider) { - $exceptionHandlerProvider.mode('log'); - var $exceptionHandler = $exceptionHandlerProvider.$get(); - $exceptionHandler('MyError'); - expect($exceptionHandler.errors).toEqual(['MyError']); - - $exceptionHandler('MyError', 'comment'); - expect($exceptionHandler.errors[1]).toEqual(['MyError', 'comment']); - })); + it('should log exceptions', function() { + module(function($exceptionHandlerProvider) { + $exceptionHandlerProvider.mode('log'); + }); + inject(function($exceptionHandler) { + $exceptionHandler('MyError'); + expect($exceptionHandler.errors).toEqual(['MyError']); + $exceptionHandler('MyError', 'comment'); + expect($exceptionHandler.errors[1]).toEqual(['MyError', 'comment']); + }); + }); + + it('should throw on wrong argument', function() { + module(function($exceptionHandlerProvider) { + expect(function() { + $exceptionHandlerProvider.mode('XXX'); + }).toThrow("Unknown mode 'XXX', only 'log'/'rethrow' modes are allowed!"); + }); + + inject(); // Trigger the tests in `module` + }); - it('should throw on wrong argument', module(function($exceptionHandlerProvider) { - expect(function() { - $exceptionHandlerProvider.mode('XXX'); - }).toThrow("Unknown mode 'XXX', only 'log'/'rethrow' modes are allowed!"); - })); }); From 98a709c2bfc1adbcdd3878ebdf98ebbee92b0733 Mon Sep 17 00:00:00 2001 From: David Souther Date: Tue, 23 Dec 2014 11:16:39 -0500 Subject: [PATCH 2/2] feat($exceptionHandlerProvider): Log errors when rethrowing. Current behavior draws a distinction between `log` and `rethrow` modes of $exceptionHandler in ngMocks. This unifies the behaviors, with both modes logging the errors while the `log` mode does not throw. Closes #10540 --- src/ngMock/angular-mocks.js | 18 +++++++++--------- test/ngMock/angular-mocksSpec.js | 13 +++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 450328a79151..1a24ea307826 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -243,31 +243,31 @@ angular.mock.$ExceptionHandlerProvider = function() { * * @param {string} mode Mode of operation, defaults to `rethrow`. * - * - `rethrow`: If any errors are passed to the handler in tests, it typically means that there - * is a bug in the application or test, so this mock will make these tests fail. * - `log`: Sometimes it is desirable to test that an error is thrown, for this case the `log` * mode stores an array of errors in `$exceptionHandler.errors`, to allow later * assertion of them. See {@link ngMock.$log#assertEmpty assertEmpty()} and * {@link ngMock.$log#reset reset()} + * - `rethrow`: If any errors are passed to the handler in tests, it typically means that there + * is a bug in the application or test, so this mock will make these tests fail. + * For any impementations that expect exceptions to be thrown, the `rethrow` mode + * will also maintain a log of thrown errors. */ this.mode = function(mode) { + switch (mode) { - case 'rethrow': - handler = function(e) { - throw e; - }; - break; case 'log': + case 'rethrow': var errors = []; - handler = function(e) { if (arguments.length == 1) { errors.push(e); } else { errors.push([].slice.call(arguments, 0)); } + if (mode === "rethrow") { + throw e; + } }; - handler.errors = errors; break; default: diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 4b09cbd84719..54ea6f340d0f 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -605,6 +605,19 @@ describe('ngMock', function() { }); }); + it('should log and rethrow exceptions', function() { + module(function($exceptionHandlerProvider) { + $exceptionHandlerProvider.mode('rethrow'); + }); + inject(function($exceptionHandler) { + expect(function() { $exceptionHandler('MyError'); }).toThrow('MyError'); + expect($exceptionHandler.errors).toEqual(['MyError']); + + expect(function() { $exceptionHandler('MyError', 'comment'); }).toThrow('MyError'); + expect($exceptionHandler.errors[1]).toEqual(['MyError', 'comment']); + }); + }); + it('should throw on wrong argument', function() { module(function($exceptionHandlerProvider) { expect(function() {