Spanish Total.js
Italiano
Documentazione Per agenti IA
Indice

Test unitari

Il test runner di total5/test: Test.push(), Test.run(), Test.print(), Test.start(), Test.fake(), Test.restore(), Test.count e Test.items.

.md GitHub
In questa pagina

Il runner Test di total5/test. La guida Test unitari spiega come usarlo, con esempi.

API

class

Test

Il test runner globale creato da require('total5/test').

Restituisce Object

Carica total5 quando non è caricato e crea l'oggetto globale Test con push(), run(), print(), start(), fake(), restore(), count e items. Esegui il file di test con node, oppure gli script di test di un progetto con total5 test (vedi Riga di comando).

Caricare
require('total5/test');

Test.push('Numbers', function(next) {
	Test.print('sum', 1 + 1 === 2 ? null : 'Wrong sum');
	next();
});

Test.run();
method

Test.push(name, fn)

Aggiunge un gruppo di test.
ParametroTipoDescrizione
name String Il nome stampato come intestazione del gruppo.
fn Function function(next)

Restituisce undefined

I gruppi vengono eseguiti nell'ordine in cui sono stati aggiunti. fn riceve next e deve chiamarlo quando il gruppo è terminato, anche dopo verifiche asincrone.

Gruppo asincrono
Test.push('Download', function(next) {
	RESTBuilder.GET('https://www.example.com').callback(function(err) {
		Test.print('GET', err);
		next();
	});
});
method

Test.run([callback])

Esegue tutti i gruppi uno alla volta.
ParametroTipoDescrizione
callback? Function function() chiamata invece di terminare il processo.

Restituisce undefined

Stampa l'intestazione di ogni gruppo, poi il numero di verifiche e il tempo. Senza callback il processo termina con codice di uscita 0 quando tutti i gruppi sono terminati.

Eseguire e fermare il server
Test.run(function() {
	F.exit();
});
method

Test.print(message, [err])

Stampa il risultato di una verifica.
ParametroTipoDescrizione
message String Il nome della verifica.
err? Error|String Il motivo di un fallimento.

Restituisce undefined

Senza err (o con un valore falsy) stampa [OK] message e conta la verifica. Con err stampa [FAIL] message, genera l'errore (una stringa diventa un Error) e termina il processo con codice di uscita 1.

Verificare un valore
var value = 'Total.js'.slug();
Test.print('slug()', value === 'total-js' ? null : 'Unexpected: ' + value);
method

Test.start(message)

Stampa un'intestazione tra due linee divisorie.
ParametroTipoDescrizione
message String Il testo dell'intestazione.

Restituisce undefined

Test.run() la chiama con il nome di ogni gruppo. Chiamala tu per separare le parti di un gruppo lungo.

Intestazione
Test.start('Second part');
method

Test.fake(name, action)

Sostituisce un'azione con una finta.
ParametroTipoDescrizione
name String Il nome dell'azione.
action Object La definizione dell'azione finta, lo stesso oggetto di NEWACTION().

Restituisce Object L'azione finta registrata.

Mette da parte l'azione name (solo la prima volta fino a Test.restore()) e registra action con lo stesso nome tramite NEWACTION(). Le chiamate di ACTION(name) eseguono allora l'azione finta. Funziona anche per un nome che non ha un'azione.

Sostituire e ripristinare
NEWACTION('Users/read', {
	action: $ => $.callback('real')
});

Test.push('Fake', async function(next) {
	Test.fake('Users/read', { action: $ => $.callback('fake') });
	var value = await ACTION('Users/read').promise();
	Test.print('fake', value === 'fake' ? null : 'Unexpected: ' + value);
	Test.restore();
	value = await ACTION('Users/read').promise();
	Test.print('restore', value === 'real' ? null : 'Unexpected: ' + value);
	next();
});

Test.run();
property

Test.count

Il numero di verifiche stampate da Test.print().

Restituisce Number

Test.run() lo stampa alla fine come Tests: <count>.

Leggere
Test.run(function() {
	console.log('Checks:', Test.count);
	process.exit(0);
});
method

Test.restore()

Rimette le azioni originali.

Restituisce undefined

Rimuove ogni azione finta registrata con Test.fake() e rimette le azioni originali. Un'azione finta di un nome che non aveva un'azione viene solo rimossa.

Dopo un gruppo
Test.push('Payments', function(next) {
	Test.fake('Payments/charge', { action: $ => $.callback({ paid: true }) });
	// ... the checks
	Test.restore();
	next();
});
property

Test.items

I gruppi aggiunti con Test.push().

Restituisce Array

Un array di oggetti { name, fn }. Test.run() li esegue uno alla volta; l'array li conserva.

Contare i gruppi
console.log(Test.items.length, 'groups');
Fatto in Europa