2017-07-27 44 views
0

我正在編寫用angularJS指令編寫的代碼的jasmine單元測試用例。以下是代碼:想要在angularjs代碼中爲addEventListener編寫jasmine單元測試用例

document.getElementById(scope.elementID).addEventListener('focus', function(e) { 
    $rootScope.isGoogleLocationProgress = true; 
    // destinationReady = true; 
}); 
document.getElementById(scope.elementID).addEventListener('keydown', function(e) { 
    $rootScope.isGoogleLocationProgress = true; 
    // destinationReady = false; 
}); 
document.getElementById(scope.elementID).addEventListener('blur', function(e) { 
    $document.on('click', checkForClearClick); 

    function checkForClearClick(e) { 
     if (e.target) { 
      if (e.target.name != "clearSearch") { 
       $timeout(getLocationPrediction, 0); 
      } 
     } 
    } 
}); 

請幫我把它添加

+0

StackOverflow不是爲您編寫代碼,而是關於如何幫助您編寫代碼。試試看,如果你需要的話,請閱讀如何測試茉莉花/角度,如果你對自己寫的東西有問題,請回來。有關如何編寫好問題的更多建議,請參閱: https://stackoverflow.com/help/how-to-ask –

回答

0

只是爲了你的聽衆使用的功能給一個名稱,單元測試這些功能。

就是這樣。

例子:

document.getElementById(scope.elementID).addEventListener('focus', function(e) { 
        $rootScope.isGoogleLocationProgress = true; 
       // destinationReady = true; 
       }); 

成爲

var yourFunc = function(e) { 
    $rootScope.isGoogleLocationProgress = true; 
    // destinationReady = true 
}; 
document.getElementById(scope.elementID).addEventListener('focus', yourFunc); 

現在可以進行單元測試功能myFunc

+0

但在我的情況下,我無法編輯角碼。因爲它已經由另一位成員編寫。我只需要爲現有代碼編寫單元測試用例。你能否分享你的意見? –

+0

我很害怕你不能單元測試你不能調用的東西。 – sjahan

相關問題