Javascript 想用angularjs代码为addEventListener编写jasmine单元测试用例吗

Javascript 想用angularjs代码为addEventListener编写jasmine单元测试用例吗,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,我正试图为用angularJS指令编写的代码编写jasmine单元测试用例。代码如下: document.getElementById(scope.elementID).addEventListener('focus', function(e) { $rootScope.isGoogleLocationProgress = true; // destinationReady = true; }); document.getElementById(scope.elementID

我正试图为用angularJS指令编写的代码编写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);
            }
        }
    }
});

请帮助我添加它

只需为侦听器中使用的函数命名,并对这些函数进行单元测试

就这样

例如:

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

,但在我的情况下,我无法编辑角度代码。因为它已经由另一个成员编写。我只是需要为现有代码编写单元测试用例。你能相应地分享你的观点吗?我很抱歉你不能对你不能调用的东西进行单元测试。StackOverflow不是为你编写代码,而是帮助你编写代码。试一试,如果需要,阅读如何测试jasmine/angular,当你写的东西有问题时再回来。有关如何编写好问题的更多建议,请参阅: