在第三方指令中测试AngularJS更改,即更改一个由第三方指令创建的元素或属性并在测试中验证更改是否成功。
以下是一个示例指令及其测试,演示如何在第三方指令中测试AngularJS更改。
指令示例:
angular.module('myApp')
.directive('myDirective', function() {
return {
restrict: 'E',
scope: {
myValue: '='
},
template: '{{myValue}}',
link: function(scope, element, attrs) {
// do something
}
};
});
测试示例:
describe('myDirective', function() {
var element, scope;
beforeEach(module('myApp'));
beforeEach(inject(function($rootScope, $compile) {
scope = $rootScope.$new();
element = angular.element(' ');
$compile(element)(scope);
scope.$digest();
}));
it('should display the initial value', function() {
scope.myValue = 'test';
scope.$digest();
expect(element.html()).toContain('test');
});
it('should update the value when changed', function() {
scope.myValue = 'test';
scope.$digest();
expect(element.html()).toContain('test');
scope.myValue = 'change';
scope.$digest();
expect(element.html()).toContain('change');
});
});
在上面的示例中,第一个测试验证初始值是否正确显示,而第二个测试更改值并验证更改是否正确显示。这些测试可确保更改在第三方指令中正常工作。