【问题标题】:unit testing angular issue with window location assign单元测试角度问题与窗口位置分配
【发布时间】:2015-02-20 00:15:54
【问题描述】:

在我的 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.

我已关注此处记录的问题 (Unit testing the AngularJS $window service),但同样的方法对我不起作用。任何的想法?

【问题讨论】:

    标签: javascript angularjs mocking mocha.js angular-mock


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2021-03-25
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多