Test
TESTER(), TEST(), ACTION() e i metodi di una richiesta di test.
In questa pagina
Gli strumenti di test di Total.js 4. La guida Test spiega come usarli con esempi.
API
method
TESTER(callback(group, start), [options])
Esegue gruppi di test in ordine e si ferma al primo fallimento.
TESTER(callback(group, start), [options])| Parametro | Tipo | Descrizione |
|---|---|---|
callback |
Function(group, start) |
Dichiara i gruppi e chiama start(). |
options? |
Object |
{ silent: true } nasconde l'output della console. |
group(name, fn(test, cleanup)) dichiara un gruppo. Al suo interno, test([name], fn(next)) dichiara un test; chiama next() in caso di successo o next(err) in caso di fallimento. cleanup(fn) imposta una funzione che viene eseguita dopo il gruppo (con l'oggetto errore { group, test, err } quando un test è fallito). start([callback]) esegue tutti i gruppi. Un fallimento chiama il cleanup del gruppo e la callback di start() con l'errore, poi lancia un Error con i nomi del gruppo e del test. I test superati stampano [OK], la fine stampa [DONE] in N ms; options.silent nasconde queste righe.
require('total4');
LOAD('config,definitions,schemas,controllers', function() {
TESTER(function(group, start) {
group('Users', function(test, cleanup) {
test('Create', function(next) {
ACTION('POST /api/users/', { name: 'Peter' }, function(err) {
next(err);
});
});
cleanup(function(err) {
// remove test data
});
});
start(function() {
process.exit(0);
});
});
});
method
ACTION(url, [data], [callback])
da 0.0.96
Invia una richiesta virtuale a una route.
ACTION(url, [data], [callback])| Parametro | Tipo | Descrizione |
|---|---|---|
url |
String |
Il metodo e il percorso, ad es. 'GET /api/products/'. |
data? |
Object |
Il body della richiesta. |
callback? |
Function(err, response) |
Chiamata con la risposta. |
Restituisce Controller Il controller della richiesta, oppure undefined quando nessuna route corrisponde.
url è 'METHOD /path/', con una query string facoltativa. Il prefisso + esegue la richiesta come autorizzata, - come non autorizzata. 'API /url/ schema' chiama un'azione di Route API. La richiesta ha l'header x-test: 1 e l'IP del server. data diventa il body. La callback riceve il risultato della route (err, response), oppure '404' quando nessuna route corrisponde. Restituisce il controller, così puoi impostare user, session o query prima che venga eseguito.
var ctrl = ACTION('+POST /api/orders/', { product: 'A1' }, function(err, response) {
console.log(err, response);
});
ctrl.user = { id: 'test' };
method
TEST(fn(add, response), [done])
da 0.0.96
Esegue un elenco di richieste virtuali e riporta il risultato.
TEST(fn(add, response), [done])| Parametro | Tipo | Descrizione |
|---|---|---|
fn |
Function(add, response) |
Dichiara le richieste. |
done? |
Function(errors, output)|Controller |
Riceve il risultato. |
Restituisce Object L'istanza del test.
add(url, name) aggiunge una richiesta (vedi ACTION()) e restituisce un'azione di test. Le richieste vengono eseguite una dopo l'altra. response raccoglie le risposte delle azioni con id(). done è una funzione (errors, output), oppure un controller: in quel caso risponde con lo stato 200 o 409 e l'output come JSON (XHR) o come pagina HTML. output è { items: [{ failed, name, error, ts }], failed }.
exports.install = function() {
ROUTE('GET /tests/', tests);
};
function tests() {
TEST(function(add, response) {
add('POST /api/products/', 'Create a product').data({ name: 'Test' }).id('product');
add('GET /api/products/', 'List products').pass(function(res) {
return res.value instanceof Array;
});
}, this);
}
method
action.data(body)
da 0.0.96
Imposta il body di una richiesta di test.
action.data(body)| Parametro | Tipo | Descrizione |
|---|---|---|
body |
Object |
Il body. |
Restituisce Object L'azione di test.
body() è un alias. Gli altri setter sono query(obj), user(obj), session(obj) e url(url). Ognuno restituisce l'azione, così puoi concatenarli.
add('+POST /api/orders/', 'Order').data({ product: 'A1' }).user({ id: 'admin', sa: true });
method
action.pass(fn(res))
da 0.0.96
Segna la richiesta come fallita a meno che fn non restituisca true.
action.pass(fn(res))| Parametro | Tipo | Descrizione |
|---|---|---|
fn |
Function(res) |
Restituisce true quando la risposta è corretta. |
Restituisce Object L'azione di test.
res ha err (anche error), value, response (tutte le risposte raccolte) e name. res.invalid(err) (anche throw()) imposta l'errore mostrato nell'output. fail(fn) è l'opposto: la richiesta fallisce quando fn restituisce true. Senza pass o fail, la richiesta fallisce quando la route restituisce un errore.
add('GET /api/products/', 'Products').pass(function(res) {
if (!res.value.length)
res.invalid('Empty list');
return res.value.length > 0;
});
method
action.make(fn(action, response))
da 0.0.96
Prepara una richiesta di test subito prima che venga eseguita.
action.make(fn(action, response))| Parametro | Tipo | Descrizione |
|---|---|---|
fn |
Function(action, response) |
Modifica l'azione prima che venga eseguita. |
Restituisce Object L'azione di test.
Usalo per costruire la richiesta a partire da risposte precedenti, per esempio un id salvato con id(name). Restituisci false per saltare la richiesta. done(fn(value)) viene eseguito in caso di successo, callback(fn(err, value)) sempre e log(fn(err, value)) prima della verifica.
add('POST /api/products/', 'Create').data({ name: 'A' }).id('created');
add('', 'Read').make(function(action, response) {
// response.created is the response of the request with id('created')
action.url('GET /api/products/' + response.created.id + '/');
});