2015-09-15 14 views
5

Vorrei un aiuto per determinare perché il mio test dell'unità in un'app sails.js non funziona come previsto.Bluebird Promise che ognuno in test moka/chai non funziona

Sto utilizzando la libreria di promemoria mocha, chai e bluebird su un'app sails.js.

Quello che voglio ottenere:

  • creare un test per il metodo TagsService.create (nome), che accetta un nome parametro.
  • prova che questo metodo non creerà un nuovo record di tag in base a nomi non validi I Pass
  • Il parametro name è obbligatorio e deve essere inferiore a 121 caratteri

Quello che ho attualmente:

// Test the 'create' method 
 
describe('Method \'create\' test result: \n', function() { 
 
    
 
    // Test that name is required and less than 121 chars long 
 
    it('Must receive the name parameter and be less than 121 chars long', function(done) { 
 
\t \t 
 
    // It should not accept any of the following names 
 
    var names = ['',' ','thisstringislongerthanthemaxof121characterslongthisstringislongerthanthemaxof121characterslongthisstringislongerthanthema',[],[{}],[{test: 'test'}],'wrongchars*[]$£%fsf','$%@~}[','£$%jkdfi',' $%"£asdwdFDE','hD8U £$&{DS ds']; 
 
    
 
    
 
     sails.bluebird.each(names,function(name){ 
 
     TagsService.create(name).then(function(data){ 
 
      assert.propertyVal(data,'status','err','An error was NOT returned - even though names provided should be invalid'); 
 
     }); 
 
     }).then(function(){ 
 
     done(); 
 
     }); 
 
    
 
\t \t 
 
    }); 
 
    
 
});

quello che succede è sembra passare, anche se passo un nome valido o restituisco null dal metodo.

risposta

5

Bene, sembra che sia riuscito a risolverlo, dopo molte prove ed errori.

Risulta necessario catturare il callback done() dalla Promessa dopo ogni metodo eseguito. Inoltre, è necessario restituire il risultato dei test eseguiti dall'oggetto promessa di TagService. (Ancora non sicuro al 100% questo è il modo corretto di pensarci ..). Ad ogni modo il test sembra funzionare correttamente ora.

Ecco il mio risultato:

var names = ['',' ','thisstringislongerthanthemaxof121characterslongthisstringislongerthanthemaxof121characterslongthisstringislongerthanthema',[],[{}],[{test: 'test'}],'wrongchars*[]$%fsf','$%@~}[','�$%jkdfi',' $%"�asdwdFDE','hD8U �$&{DS ds']; 
 
\t \t \t 
 
sails.bluebird.each(names, function(name){ 
 
    return TagsService.create(name).then(function(data) { 
 
\t assert.property(data, 'status', 'create method did not return a status property'); 
 
\t assert(data.status === 'err', 'even with an invalid name parameter passed - it did not return an err status, which it must do with an invalid name.'); 
 
    }); 
 
}).then(function(){ 
 
\t done(); 
 
}).catch(done);

+0

Vedi anche https://github.com/domenic/chai-as-promised –

Problemi correlati