[英]Unit testing watchers in angular js controller
如何使用jasmine對此代碼段進行單元測試?
$scope.profileObject = ProfilesSharedObject;
$scope.$watch("profileObject.startDate", function() {
var startDate = $scope.profileObject.startDate._d;
var endDate = $scope.profileObject.endDate._d;
var newStartDate = moment(startDate).format("YYYY-MM-DD");
var newEndDate = moment(endDate).format("YYYY-MM-DD");
$scope.startDate = moment(startDate).format("MM/DD");
$scope.endDate = moment(endDate).format("MM/DD/YYYY");
$scope.getSleepData(newStartDate, newEndDate);
});
其中ProfileSharedObject是一個角度js服務
每個摘要周期都會評估監聽監聽器。 通常這種情況會自動發生,但在進行單元測試時,您需要手動觸發它:
it('should update the start date', function() {
// Arrange
ProfileSharedObjectMock.startDate = new Date(2013, 0, 1);
// Act
$scope.$digest();
// Assert
expect($scope.startDate).toEqual(new Date(2013, 0, 1));
});
我已經創建了一個Plunker腳本,因此您可以看到整個測試套件正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.