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 に答える