2015-04-14 4 views
5

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); 
    }); 
+0

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

+0

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

risposta

2

Questo è ovviamente in base alla progettazione, i test devono controllare che le chiamate HTTP sono in corso e che stanno richiedendo l'URL corretto. Invece di controllare se le richieste vengono fatte a /\.html$/ perché non controllare invece se le richieste vengono fatte agli endpoint corretti? Che si tratti di una direttiva parziale o di una chiamata API.

Se si insiste a buttare via quello che potrebbe essere un test utile, è possibile spostare il whenGET() in un beforeEach().

+0

Sto leggendo il documento su https://docs.angularjs.org/api/ngMock/service/$httpBackend, ma mi chiedo ancora quale sia la differenza tra whenGET e expectGET. Comprendo expectGET è la definizione di richieste simulate. È quando GET sta facendo lo stesso tipo di lavoro? – Stephane

+0

Queste due funzioni sono utilizzate per il controllo o la simulazione? – Stephane

+0

Il mio servizio non dovrebbe chiamare alcuna risorsa html in primo luogo. Si tratta solo di un endpoint JSON. Non so cosa potrei fare per non gettare via questa cosa e rendere più utile il test. – Stephane

Problemi correlati