AngularJS
Unit-Tests
Suche…
Bemerkungen
Dieses Thema enthält Beispiele für das Testen von Einheiten der verschiedenen Konstrukte in AngularJS. Unit-Tests werden oft mit Jasmine geschrieben , einem beliebten verhaltensorientierten Test-Framework. Beim Komponententest für Winkelkonstrukte müssen Sie ngMock als Abhängigkeit beim Ausführen der Komponententests angeben .
Gerätetest einen Filter
Filtercode:
angular.module('myModule', []).filter('multiplier', function() {
return function(number, multiplier) {
if (!angular.isNumber(number)) {
throw new Error(number + " is not a number!");
}
if (!multiplier) {
multiplier = 2;
}
return number * multiplier;
}
});
Der Test:
describe('multiplierFilter', function() {
var filter;
beforeEach(function() {
module('myModule');
inject(function(multiplierFilter) {
filter = multiplierFilter;
});
});
it('multiply by 2 by default', function() {
expect(filter(2)).toBe(4);
expect(filter(3)).toBe(6);
});
it('allow to specify custom multiplier', function() {
expect(filter(2, 4)).toBe(8);
});
it('throws error on invalid input', function() {
expect(function() {
filter(null);
}).toThrow();
});
});
Anmerkung: Bei dem inject
Aufruf im Test muss Ihr Filter durch seinen Namen + Filter angegeben werden . Der Grund dafür ist, dass Angular jedes Mal, wenn Sie einen Filter für Ihr Modul registrieren, diesen mit einem an seinen Namen angehängten Filter
registriert.
Komponententest einer Komponente (1.5+)
Komponentencode:
angular.module('myModule', []).component('myComponent', {
bindings: {
myValue: '<'
},
controller: function(MyService) {
this.service = MyService;
this.componentMethod = function() {
return 2;
};
}
});
Der Test:
describe('myComponent', function() {
var component;
var MyServiceFake = jasmine.createSpyObj(['serviceMethod']);
beforeEach(function() {
module('myModule');
inject(function($componentController) {
// 1st - component name, 2nd - controller injections, 3rd - bindings
component = $componentController('myComponent', {
MyService: MyServiceFake
}, {
myValue: 3
});
});
});
/** Here you test the injector. Useless. */
it('injects the binding', function() {
expect(component.myValue).toBe(3);
});
it('has some cool behavior', function() {
expect(component.componentMethod()).toBe(2);
});
});
Gerätetest einer Steuerung
Controller-Code:
angular.module('myModule', [])
.controller('myController', function($scope) {
$scope.num = 2;
$scope.doSomething = function() {
$scope.num += 2;
}
});
Der Test:
describe('myController', function() {
var $scope;
beforeEach(function() {
module('myModule');
inject(function($controller, $rootScope) {
$scope = $rootScope.$new();
$controller('myController', {
'$scope': $scope
})
});
});
it('should increment `num` by 2', function() {
expect($scope.num).toEqual(2);
$scope.doSomething();
expect($scope.num).toEqual(4);
});
});
Testen Sie einen Dienst
Service code
angular.module('myModule', [])
.service('myService', function() {
this.doSomething = function(someNumber) {
return someNumber + 2;
}
});
Der Test
describe('myService', function() {
var myService;
beforeEach(function() {
module('myModule');
inject(function(_myService_) {
myService = _myService_;
});
});
it('should increment `num` by 2', function() {
var result = myService.doSomething(4);
expect(result).toEqual(6);
});
});
Unit-Test eine Anweisung
Richtliniencode
angular.module('myModule', [])
.directive('myDirective', function() {
return {
template: '<div>{{greeting}} {{name}}!</div>',
scope: {
name: '=',
greeting: '@'
}
};
});
Der Test
describe('myDirective', function() {
var element, scope;
beforeEach(function() {
module('myModule');
inject(function($compile, $rootScope) {
scope = $rootScope.$new();
element = angular.element("<my-directive name='name' greeting='Hello'></my-directive>");
$compile(element)(scope);
/* PLEASE NEVER USE scope.$digest(). scope.$apply use a protection to avoid to run a digest loop when there is already one, so, use scope.$apply() instead. */
scope.$apply();
})
});
it('has the text attribute injected', function() {
expect(element.html()).toContain('Hello');
});
it('should have proper message after scope change', function() {
scope.name = 'John';
scope.$apply();
expect(element.html()).toContain("John");
scope.name = 'Alice';
expect(element.html()).toContain("John");
scope.$apply();
expect(element.html()).toContain("Alice");
});
});
Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow