2

在我的 ctrl 中,我有这段代码可以在函数完成工作后导航到页面。

 $scope.myFunction = function(){
     //other code
     window.location.assign(url);
 }

我的测试代码如下所示。

   describe('TEST', function () {
      var window;

      beforeEach(module(function($provide) {  
       //dummy window    
       window = {            
           location:{href:function(){return 'dummy'},assign:function()
            {return 'dummy1'}}   
        };
       

         // We register our new $window instead of the old    
            $provide.constant('$window',window);
       
         }));

       it("should test",function(){
            $scope.myfunction();
       });
});

  The error that I get is ..Some of your tests did a full page reload! and the test fails.

我已经关注了此处记录的问题(单元测试 AngularJS $window 服务),但同样的方法对我不起作用。任何想法?

4

1 回答 1

2

您的函数需要注入 $window,而不是 window

$scope.myFunction = function($window){
     //other code
     $window.location.assign(url);
 }

正如所写,您绕过了模拟的 $window 服务并作用于实际的本机窗口对象。

于 2015-02-20T02:11:40.073 回答