单元测试观察家的角度JS控制器 [英] Unit testing watchers in angular js controller
本文介绍了单元测试观察家的角度JS控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个片段code怎么能单位利用茉莉测试?
How can this snippet code be unit tested using 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服务
where ProfileSharedObject is a angular js service
推荐答案
手表监听器在每个周期消化评价。通常出现这种情况自动的,但在单元测试,您需要手动触发它:
Watch listeners are evaluated at every digest cycle. Usually that happens automagically, but while unit testing you need to manually trigger it:
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脚本的,所以你可以看到整个测试套件的工作。
I've created a Plunker script so you can see the whole test suite working.
这篇关于单元测试观察家的角度JS控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文