This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +22
-9
lines changed
2 files changed +22
-9
lines changed Original file line number Diff line number Diff line change @@ -243,31 +243,31 @@ angular.mock.$ExceptionHandlerProvider = function() {
243
243
*
244
244
* @param {string } mode Mode of operation, defaults to `rethrow`.
245
245
*
246
- * - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
247
- * is a bug in the application or test, so this mock will make these tests fail.
248
246
* - `log`: Sometimes it is desirable to test that an error is thrown, for this case the `log`
249
247
* mode stores an array of errors in `$exceptionHandler.errors`, to allow later
250
248
* assertion of them. See {@link ngMock.$log#assertEmpty assertEmpty()} and
251
249
* {@link ngMock.$log#reset reset()}
250
+ * - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
251
+ * is a bug in the application or test, so this mock will make these tests fail.
252
+ * For any implementations that expect exceptions to be thrown, the `rethrow` mode
253
+ * will also maintain a log of thrown errors.
252
254
*/
253
255
this . mode = function ( mode ) {
256
+
254
257
switch ( mode ) {
255
- case 'rethrow' :
256
- handler = function ( e ) {
257
- throw e ;
258
- } ;
259
- break ;
260
258
case 'log' :
259
+ case 'rethrow' :
261
260
var errors = [ ] ;
262
-
263
261
handler = function ( e ) {
264
262
if ( arguments . length == 1 ) {
265
263
errors . push ( e ) ;
266
264
} else {
267
265
errors . push ( [ ] . slice . call ( arguments , 0 ) ) ;
268
266
}
267
+ if ( mode === "rethrow" ) {
268
+ throw e ;
269
+ }
269
270
} ;
270
-
271
271
handler . errors = errors ;
272
272
break ;
273
273
default :
Original file line number Diff line number Diff line change @@ -605,6 +605,19 @@ describe('ngMock', function() {
605
605
} ) ;
606
606
} ) ;
607
607
608
+ it ( 'should log and rethrow exceptions' , function ( ) {
609
+ module ( function ( $exceptionHandlerProvider ) {
610
+ $exceptionHandlerProvider . mode ( 'rethrow' ) ;
611
+ } ) ;
612
+ inject ( function ( $exceptionHandler ) {
613
+ expect ( function ( ) { $exceptionHandler ( 'MyError' ) ; } ) . toThrow ( 'MyError' ) ;
614
+ expect ( $exceptionHandler . errors ) . toEqual ( [ 'MyError' ] ) ;
615
+
616
+ expect ( function ( ) { $exceptionHandler ( 'MyError' , 'comment' ) ; } ) . toThrow ( 'MyError' ) ;
617
+ expect ( $exceptionHandler . errors [ 1 ] ) . toEqual ( [ 'MyError' , 'comment' ] ) ;
618
+ } ) ;
619
+ } ) ;
620
+
608
621
it ( 'should throw on wrong argument' , function ( ) {
609
622
module ( function ( $exceptionHandlerProvider ) {
610
623
expect ( function ( ) {
You can’t perform that action at this time.
0 commit comments