4

如何使用 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 服务

4

1 回答 1

5

在每个摘要周期评估监视侦听器。通常这是自动发生的,但是在单元测试时你需要手动触发它:

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 脚本,因此您可以看到整个测试套件正在运行。

于 2013-09-17T04:26:45.217 回答