Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 224d7d6

Browse files
fredsamhevery
authored andcommitted
docs(exceptionHandler): document testing
Update src/ng/exceptionHandler.js Here's an iniitla attempt at documenting how one might write a test using $exceptionHandlerProvider. The key take-away is the use of this pattern: it(...  module(...    $exceptionHandlerProvider.mode('log');  });  inject(...  ); });
1 parent a179a9a commit 224d7d6

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ng/exceptionHandler.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
* the browser console.
1212
*
1313
* In unit tests, if `angular-mocks.js` is loaded, this service is overridden by
14-
* {@link ngMock.$exceptionHandler mock $exceptionHandler}
14+
* {@link ngMock.$exceptionHandler mock $exceptionHandler} which aids in testing.
1515
*
1616
* @param {Error} exception Exception associated with the error.
1717
* @param {string=} cause optional information about the context in which
1818
* the error was thrown.
19+
*
1920
*/
2021
function $ExceptionHandlerProvider() {
2122
this.$get = ['$log', function($log){

src/ngMock/angular-mocks.js

+24
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,30 @@ angular.mock.$Browser.prototype = {
202202
* Mock implementation of {@link ng.$exceptionHandler} that rethrows or logs errors passed
203203
* into it. See {@link ngMock.$exceptionHandlerProvider $exceptionHandlerProvider} for configuration
204204
* information.
205+
*
206+
*
207+
* <pre>
208+
* describe('$exceptionHandlerProvider', function() {
209+
*
210+
* it('should capture log messages and exceptions', function() {
211+
*
212+
* module(function($exceptionHandlerProvider) {
213+
* $exceptionHandlerProvider.mode('log');
214+
* });
215+
*
216+
* inject(function($log, $exceptionHandler, $timeout) {
217+
* $timeout(function() { $log.log(1); });
218+
* $timeout(function() { $log.log(2); throw 'banana peel'; });
219+
* $timeout(function() { $log.log(3); });
220+
* expect($exceptionHandler.errors).toEqual([]);
221+
* expect($log.assertEmpty());
222+
* $timeout.flush();
223+
* expect($exceptionHandler.errors).toEqual(['banana peel']);
224+
* expect($log.log.logs).toEqual([[1], [2], [3]]);
225+
* });
226+
* });
227+
* });
228+
* </pre>
205229
*/
206230

207231
angular.mock.$ExceptionHandlerProvider = function() {

0 commit comments

Comments
 (0)