Tutti i miei test UNIT, non E2E, eseguono un rootScope.digest esplicito () o httpBackend.flush() per svuotare il callback asincrono, sperimentare l'errore:Come evitare l'errore: richiesta imprevista: OTTENI 'ogni volta che un test AngularJS esegue un rootScope.digest() o httpBackend.flush()
How to avoid the 'Error: Unexpected request: GET'
No more request expected
mi sa che è perché httpBackend chiama il modello ui-router. Non so perché lo voglia fare. Non sto chiedendo questo. Voglio solo che chiami il mio servizio beffato di json.
Questo errore mi costringe ad avere la seguente dichiarazione in ogni blocco di esso():
$httpBackend.whenGET(/\.html$/).respond('');
Ci deve essere un modo più ordinato.
Specialmente se il test non ha alcun uso del $ httpBackend, in primo luogo:
it('should return the list of searched users', function() {
// Always use this statement so as to avoid the error from the $http service making a request to the application main page
$httpBackend.whenGET(/\.html$/).respond('');
var users = null;
UserService.search('TOTO', 1, 10, 'asc', function(data) {
users = data.content;
});
$rootScope.$digest();
expect(users).toEqual(RESTService.allUsers.content);
});
I passi di prova, ma sembra hacker. O noobish :-)
EDIT: Un altro test:
it('should return the list of users', function() {
// Always use this statement so as to avoid the error from the $http service making a request to the application main page
$httpBackend.whenGET(/\.html$/).respond('');
// Create a mock request and response
$httpBackend.expectGET(ENV.NITRO_PROJECT_REST_URL + '/users/1').respond(mockedUser);
// Exercise the service
var user = null;
RESTService.User.get({userId: 1}).$promise.then(function(data) {
user = data;
});
// Synchronise
$httpBackend.flush();
// Check for the callback data
expect(user.firstname).toEqual(mockedUser.firstname);
expect(user.lastname).toEqual(mockedUser.lastname);
});
Direi che il test è stato assolutamente effettuare una chiamata "$ http" qui. Perché in realtà non si aspetta una chiamata all'endpoint API dell'utente? – juco
Sì, effettua una richiesta http. Ma anche molti dei miei test. Non ho ricevuto la tua domanda. Modificherò la domanda per aggiungere un altro test che mostri la stessa soluzione. – Stephane