0

私は最後に、私が書いた古いanglejsアプリケーションを使ってテストする方法を学んでいます。私はコントローラにいくつかのモーダルを持っており、私の人生のために、 'modalInstance.result.then'のコードが実行されていることを確認する方法を理解できません。testing modalInstance.result.then

私はGoogleとSOを検索し、モーダルをテストしている人の例を見つけましたが、これまではすべてモーダルコントローラ自体をテストするように見えました。

私はその約束(modalInstance.result.then)を解決する方法を教えてください。私は$ modal.close()を実行しようとしましたが、それはエラーに失敗します。私は、modalInstanceと$ modalをいくつかの方法で嘲笑してみました。ジャスミンのスパイスなどを使用していました。テストに関する私の無知は私を抱きしめています。どんな援助も実現されるだろう。ここで

は私controller.jsです:ここでは

(function() { 
    var comment = angular.module('APP.comment', ['APP.user']); 

    var commentController = function($scope, $modal) { 

     var self = this; 

     self.addComment = function(newComment) { 

     var modalInstance = $modal.open({ 
      templateUrl: 'views/commentModal.html', 
      backdrop: 'static', 
      windowClass: 'modal', 
      controller: 'commentModalController', 
      controllerAs: 'commentCtrl', 
      resolve: { 
       newComment: function() { 
        return newComment; 
       } 
      } 
     }); 

     modalInstance.result.then(function(data) { 
      // How do I test that the function or branches here 
      // were run? 
      if (data.length === 2) { 
       // do this thing 
      } else { 
       // do this other thing 
      } 
     }); 
     }; 
    }; 

    commentController.$inject = ['$scope', '$modal']; 
    comment.controller('commentController', commentController); 
}()); 

は、私が持っているテストは、これまでのところです:

describe('Unit: commentController', function() { 

    var $rootScope, 
     $scope, 
     $controller, 
     $modal; 

    beforeEach(module('APP.comment')); 

    beforeEach(inject(function(_$rootScope_, _$controller_, _$modal_) { 

     $modal = _$modal_; 

     $rootScope = _$rootScope_; 
     $scope = $rootScope.$new(); 

     $controller = _$controller_('commentController as commentCtrl', { 
      $scope: $scope, 
      $modal: $modal, 
     }); 

    })); 

    it('should have controller defined', function() { 
     expect($scope.qaCtrl).toBeDefined(); 
    }); 

    it('should have method defined', function() { 
     expect($scope.qaCtrl.addComment).toBeDefined(); 
    }); 

    describe('$scope.commentCtrl.addComment', function() { 
     it('should open modal', function() { 
      $scope.commentCtrl.addComment(); 
     }); 
    }); 
}); 

私はここにplnkr持っている:
http://plnkr.co/edit/YtYVPReH9yysZXPjbsC0?p=preview

答えて

3
it('should open modal', inject(function($q) { 
    var fakeResult = { 
    result: $q.when([]) 
    }; 
    spyOn($modal, 'open').and.returnValue(fakeResult); 

    $scope.commentCtrl.addComment(); 

    $scope.$apply(); 

    // now check that the right thing has been done, given the empty array returned 
})); 
+0

トリックをした、答えのおかげで! – Woody2143