2017-03-15 2 views
0

私はジャスミンとカルマで私のangularjsコントローラをテストしようとしていますが、スイートアラート機能のテストコードブロックはできません。私のテストクラスから$ scope.getCategory()が呼び出されたかどうかをテストするにはどうすればよいでしょうか?ここに私のコントローラとジャスミンのテストケースのコード例があります。ジャスミンテストでsweetalertを使用したAngularJsユニットテストコントローラ

コントローラー:

$scope.changeCategoryStatus = function(selectedId, selectedActive, topId) { 
    sweet.show({ 
     title : "Kategoriyi "+(!selectedActive ? 'aktif' : 'pasif')+ " hale getirmek istiyor musunuz?", 
     type : "warning", 
     showCancelButton : true, 
     confirmButtonColor : "#DD6B55", 
     confirmButtonText : "Evet, değiştir!", 
     closeOnConfirm : false, 
     showLoaderOnConfirm : true, 
     html : false 
    }, function() { 
     $http({ 
      method : "POST", 
      url : webRootUrl+"ajax/category/setActivation", 
      data : { 
       "id" : selectedId, 
       "active" : !selectedActive 
      } 

     }).then(function(response) { 

      //console.log(JSON.stringify(response.data)) 
      if(response.data.outPutDouble==-7){ 
       swal("Değiştirilemedi!", response.data.outPutString,"error"); 
      }else{ 
      $scope.getCategory(topId); 
      swal("Bu kategorinin durumu değiştirildi","", 
      "success"); 
      } 
     }, function myError(response) { 
      swal("Bu kategorinin durumu değiştirilemedi","","error"); 

      //console.log("***aaa" + JSON.stringify(response)) 
     }); 


    }); 

} 

ジャスミンテストケース:

it("changeCategoryStatus success", function() { 
    $scope.changeCategoryStatus(21,true,0) 

    spyOn($scope,'getCategory') 
    expect($scope.getCategory).toHaveBeenCalled(); }); 

誰が先に同様の問題がありましたか?事前におかげで助けてください。ここで

答えて

0

は、あなたがそれをまっすぐ進む空想何も見ることができるように、その中にsweetAlertと

  • コール機能
  • は遅延を追加し、私は私の角度2のアプリユニットテスト

    it("should delete token",() => { 
        spyOn(localStorage, 'removeItem'); 
    
        // call function 
        comp.deleteToken(); 
    
        // add some delay till the sweetAlert modal show up 
        setTimeout(() => { 
        // select the button for the activity you want to test 
        let cancelSwal: HTMLButtonElement = fixture.debugElement.query(By.css(".sa-button-container > .cancel")).nativeElement; 
        cancelSwal.click(); 
    
        // test your sweetAlert callback function 
        expect(localStorage.removeItem).toHaveBeenCalledWith("token"); 
        }, 100); 
    }); 
    

    でやったことですsetTimeoutでスワールモーダルがあることを確認する

  • CSSセレクタでDOM要素を選択する
  • あなたが
  • ハッピーテストをテストしたいあなたのコールバック機能:)
に到達するために(我々の場合には、それは clickイベントです)必要なアクションを実行します。
関連する問題