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