2014-04-28 14 views
5

Sono appena arrivato al framework di test jasmine js e ho ottenuto alcuni risultati strani oggi.Jasmine array.length expect

Vedere il seguente codice (search è una funzione che preforme una richiesta API e restituisce una promessa):

it('should be able to search', function() { 
    search('string').done(function(result) { 
     expect(result.length).toBeGreaterThan(1); //true 
     console.log(result.lenght); // undefined 
    }); 
}); 

Il fatto è che, a causa di alcuni errori che devo correggere, il risultato dalla la promessa non è definita, ma il test è contrassegnato come Success. Trovo che questo sia fuorviante e se non approfondirò questo argomento avrei creduto che il test fosse un successo mentre chiaramente non lo era. È questo comportamento previsto?

risposta

10

Hai errori di battitura in console.log (risultato.lenght) per favore prova questo.

it('should be able to search', function() { 
search('string').done(function(result) { 
    expect(result.length).toBeGreaterThan(1); //true 
    console.log(result.length); // undefined 
}); 
}); 
3

Per il test asincroni funzioni, i test devono essere scritte in modo leggermente diverso. Dalle ultime Jasmine (2.0) documentation, prova asincrona sono scritti nel modo seguente:

beforeEach(function(done) { 
    setTimeout(function() { 
     // do setup for spec here 

     // then call done() in beforeEach() to start asynchronous test 
     done(); 
    }, 1); 
}); 

it('should be able to search', function(done) { 
    search('string').done(function(result) { 
     expect(result.length).toBeGreaterThan(1); //true 

     // call done() in the spec when asynchronous test is complete 
     done(); 
    }); 
}); 
+0

ho deciso di smettere di cercare un modo per fare requirejs test basato come sto diventando un po 'irritato, cercherà di nuovo in un paio di giorni/settimane. Grazie per il tuo contributo! – MegaWubs