Làm thế nào về vấn đề này cho chế giễu một hình thức AngularJS và sau đó thử nghiệm có dạng $ bẩn và $ trạng thái hợp lệ:
// example usage of html form element
<form data-ng-submit="update()" name="optionsForm" novalidate="novalidate">
// example usage html button element
<button type="submit" ng-disabled="!canSave()">Update Options</button>
// Controller check if form is valid
$scope.canSave = function() {
return $scope.rideshareForm.$dirty && $scope.rideshareForm.$valid;
};
// Unit Test
// scope is injected in a beforeEach hook
it('$scope.canSave returns true if an options form is valid or false if non-valid', function() {
// mock angular form
scope.optionsForm = {};
// valid form
scope.optionsForm.$dirty = true;
scope.optionsForm.$valid = true;
expect(scope.canSave()).toBe(true);
// non-valid form
scope.rideshareForm.$dirty = true;
scope.rideshareForm.$valid = false;
expect(scope.canSave()).toBe(false);
});
Nguồn
2013-11-06 05:04:47