2016-04-19 10 views
0

Ich habe Probleme mit ngMocks und der $log Anbieter, der dort verspottet wird.

Jedesmal, wenn ein Einsatz $log in meinem Code, die Tests fehlschlägt, weil der Mock für $log scheitern an (Linie 295):

var $log = { 
    log: function() { $log.log.logs.push(concat([], arguments, 0)); }, 
    warn: function() { $log.warn.logs.push(concat([], arguments, 0)); }, 
    info: function() { $log.info.logs.push(concat([], arguments, 0)); }, 
    error: function() { $log.error.logs.push(concat([], arguments, 0)); }, 
    debug: function() { 
    if (debug) { 
     $log.debug.logs.push(concat([], arguments, 0)); 
    } 
    } 
}; 

als $log.info.logs sieht nicht definiert und nicht ein Array zu sein.

Ich habe bemerkt, dass die Log-Funktion in ngMock auf diese Weise zu ändern:

info: function() { 
    $log.info.logs = $log.info.logs || []; 
    $log.info.logs.push(concat([], arguments, 0)); 
    }, 

meinen Testdurchlauf machen.

Irgendeine Idee, warum das passieren kann? Ich denke, es ist kein Fehler in ngMock, da ich keine Referenz gefunden habe.

+0

Beliebig darüber, wie dieses Problem beheben? –

Antwort

3

Das gleiche Problem passiert hier.

Es begann zu passieren, sobald ich einen Dekorator auf $ log hinzugefügt, um Debug-Anrufe zu interpaten. Es passiert, wenn Sie log Debug in einer Initialisierungsphase nur einer Winkelkomponente aufrufen.

Ich Workaround es zu überprüfen, ob die Mock-Implementierung vorhanden ist (in Jasmin-Tests) und Aufruf zurückgesetzt, um das Array zu erstellen erwartet.

$provide.decorator('$log', function ($delegate) { 
    // Keep track of the original debug method, we'll need it later. 
    var debugFn = $delegate.debug; 
    /* 
    * Intercept the call to $log.debug() so we can add on 
    * our enhancement. We're going to add on a date and 
    * time stamp to the message that will be logged. 
    */ 
    $delegate.debug = function() { 

     var args = [].slice.call(arguments); 
     args[0] = ['Debug', ' - ', new Date().toString(), ': ', args[0]].join(''); 

     // HACK awfull fix for angular mock implementation whithin jasmine test failing issue 
     if (typeof $delegate.reset === 'function' && !($delegate.debug.logs instanceof Array)) { 
      // if we are within the mock and did not reset yet, we call it to avoid issue 
      // console.log('mock log impl fix to avoid logs array not existing...'); 
      $delegate.reset(); 
     } 

     // Send on our enhanced message to the original debug method. 
     debugFn.apply(null, arguments); 
    }; 

    return $delegate; 
}); 

Hope this helps ...