Ho il seguente comando (accorgo che al momento esemplificazione faccio una chiamata esplicita a $scope.getNotifications()
):
bla.controller("myctrl", [
"$scope", "$http", "configs", function ($scope, $http, configs) {
$scope.getNotifications = function() {
$http.get("bla/blabla").success(function (data) {
});
};
$scope.removeNotification = function (notification) {
var index = $scope.allNotifications.indexOf(notification);
$scope.allNotifications.splice(index, 1);
};
$scope.getNotifications();
}
]);
poi faccio alcuni test di unità (si noti che il controllore viene istanziato nella prima di ciascuno):
describe("blaController", function() {
var scope, $httpBackend;
beforeEach(module('bla'));
beforeEach(inject(function ($controller, $rootScope, _$httpBackend_) {
scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
$controller('blaCtrl', { $scope: scope });
}));
afterEach(function(){
//assert
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it("should get all notifications from server when instantiated", function() {
//arrange
$httpBackend.expectGET("api/v1/notifications").respond(200, {});
$httpBackend.flush();
//act - done implicitly when controller is instantiated
});
it("should store all notifications from server on the client when success call to server", function() {
//arrange
$httpBackend.whenGET("api/v1/notifications").respond(200, [{ a: 1, b: 2 }, { c: 3, d: 4 }]);
$httpBackend.flush();
//act - done implicitly when controller is instantiated
//assert
expect(scope.allNotifications).toEqual([{ a: 1, b: 2 }, { c: 3, d: 4 }]);
});
Tutto è andato bene fino ad ora. Tutti i test passano Ma quando aggiungo un nuovo test (vedi sotto) che non richiede alcuna chiamata HTTP fallisce perché nello afterEach()
verifica per le aspettative ma non ci sono aspettative impostate nello removeNotification()
. Questo è il messaggio di errore dal karma: PhantomJS 1.9.7 (Windows 8) notificationCenterController removeNotification dovrebbero rimuovere la data di notifica da th e lista FALLITA Errore: richiesta imprevisto: GET API/v1/notifiche Non più richiesta dovrebbe
it("should remove the given notification from the list", function() {
//arrange
var targetObj = { a: 2 };
scope.allNotifications = [{ a: 1 }, targetObj, { a: 3 }];
//act
scope.removeNotification(targetObj);
//assert
expect(scope.allNotifications).toEqual([{ a: 1 }, { a: 3 }]);
});
La maggior parte del mio test ha chiamate http, quindi è consigliabile inserire la verifica in afterEach. Mi chiedevo quale altra opzione dovrei evitare di copiare incollare il corpo afterEach nei test N-1. C'è un modo per dire a $httpBackend
di ignorare eventuali chiamate?
Nel nostro progetto abbiamo spostato le chiamate $ http a una fabbrica e deriso quella fabbrica nei test del controller. In questo modo è possibile testare la fabbrica utilizzando $ httpBackend e testare la logica del controller senza $ httpBackend. – knutesten
Il controller ha '$ scope.getNotifications();' sulla sua ultima riga in modo che vengano effettuate chiamate http. Stai iniziando il controller in un 'beforeEach', quindi verrà ricreato per ogni singolo test e quindi eseguirà anche la chiamata a' $ http' in ogni singolo test. – ivarni
Sì hai ragione Ho aggiornato la risposta per essere più chiara.So che sono stati fatti ma non per il metodo in questione che voglio testare, quindi non cerco aspettative nel test, ma le verifiche avvengono ancora da quando sono state fatte dopo ogni test. –