2015-08-25 31 views
0

我試圖測試下面的代碼,它只在$scope.checkOutDateundefined時才運行。

$scope.checkInUpdatesCheckOutDate = function(checkOutViewValue){ 
    var checkInDateInMilliseconds = new Date($scope.checkInDate).getTime(), 
    InMilliseconds = new Date(checkOutViewValue).getTime(), 
    diffBetweenCheckOutAndCheckIn = Math.floor((checkOutDateInMilliseconds - checkInDateInMilliseconds)/oneDay); 

    if(diffBetweenCheckOutAndCheckIn > 0){ 
    $scope.$apply(function(){ 
     $scope.checkOutDate = $filter('date')(new Date(checkOutViewValue).getTime(), 'EEEE, M/dd/yyyy'); 
     $scope.nights = diffBetweenCheckOutAndCheckIn; 
    }); 
    } 
} 

但是,當我跑我的茉莉花試驗,並測試$ scope.checkOutDate不會從不確定的改變。我的功能應該設置$scope.checkOutDate等於checkOutViewValue(視圖中輸入的當前值)。

這裏是我的測試:你的函數調用之後

describe('checkInUpdatesCheckOutDate', function(){ 

     it('should work', function(){ 
     $scope.checkInDate = '9/17/15'; 
     var checkOutViewValue = '9/20/15'; 
     var result = $filter('date')(new Date(checkOutViewValue).getTime(), 'EEEE, M/dd/yyyy'); 
     $scope.checkInUpdatesCheckOutDate(); 

     expect($scope.checkOutDate).toEqual(result); 
     }); 
    }); 

回答

0

呼叫$scope.$apply()。這應該會觸發'摘要'循環並執行'apply'塊中的代碼。

+0

並沒有爲我工作,可惜的是:( – pokrishka

0

而不是把代碼中的$scope.$apply函數內部的,只需要調用$scope.$digest()在它的位置