mardi 24 mars 2020

Testing try/catch in Jasmine is not working

I have this piece of code in AngularJS that I'm trying to test:

var _savePendingChanges = function() {
  try {
    $rootScope.$broadcast('externalSave');
  } catch (e) {
    // Sometimes, AngularJS throws a "Cannot read property $$nextSibling of
    // null" error. To get around this we must use $apply().
    $rootScope.$apply(function() {
      $rootScope.$broadcast('externalSave');
    });
  }
}

Here's the test I've wrote until now, and it's not working (it throw the error from the errorMessage and a Jasmine error: Error: Expected function to throw an Error.):

fit('should save pending changes', function() {
    var errorMessage = 'Cannot read property $$nextSibling of null'; 
    spyOn($rootScope, '$broadcast').and.throwError(errorMessage);
    expect(function() {
      RouterService.savePendingChanges();
    }).toThrowError(errorMessage);
  });

Can anyone give me a hand on this? Thanks!

Aucun commentaire:

Enregistrer un commentaire