2015-04-08 36 views
2

我正在为我的一些函数编写单元测试,而测试运行器似乎对绑定函数有问题。我绑定了一个函数,所以我在内部函数中引用了这个。下面是代码:Angular,karma/jasmine tests not handling .bind

    loadStates: function(name, stateName, options) { 

        if (myModule.getModule(name) !== undefined) { 
         this.prepState(name, stateName, options); 

        } else { 
         var bindForCheck = this.prepState.bind(this); 

         //module cannot be found check for 5 seconds 
         $log.warn("Requesting " + name + "..."); 
         var timeToCheck = true; 
         setTimeout(function() { 
          timeToCheck = false; 
         }, 5000); 
         var check = { 
          init: function() { 
           check.checkAgain(); 
          }, 
          checkAgain: function() { 
           if (timeToCheck) { 
            if (myModule.getModule(name) !== undefined) { 
             bindForCheck(name, stateName, options); 
            } else { 
             //still doesn't exists 
             setTimeout(check.checkAgain, 200); 
            } 
           } else { 
            //doesn't exist after 5 seconds 
            $log.error("Requested module (" + name + ") could not be found at this time."); 
           } 
          } 
         }; 
         check.init(); 
        } 

       } 

所以这个问题是与

var bindForCheck = this.prepState.bind(this); 

刚刚让我叫check.checkAgain()函数中的外部函数。当我尝试运行功能

TypeError: 'undefined' is not a function (evaluating 'this.prepState.bind(this)') 

在这里可以使用一些帮助的其他部分

测试运行是吐回这个错误,这我难倒如何解决这一问题。谢谢!

回答

4

您可能正在使用版本<的PhantomJS 2.以下是github上的issue。你将不得不更新PhantomJS或使用polyfill。有一个bower package应该做的伎俩。