---
title: Test
project: Total.js V4
version: 0.0.99
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v4/0-0-99/reference/testing/
updated: 2026-11-18T16:57:44.579Z
---

# Test

> TESTER(), TEST(), ACTION() e i metodi di una richiesta di test.

Gli strumenti di test di Total.js 4. La guida [Test](../../guides/testing/) spiega come usarli con esempi.

## API

### TESTER(callback(group, start), [options])

Type: `method`

Esegue gruppi di test in ordine e si ferma al primo fallimento.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function(group, start)` | yes |  | Dichiara i gruppi e chiama `start()`. |
| `options` | `Object` | no |  | `{ 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.

Example: tests/index.js
```js
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);
		});
	});
});
```

### ACTION(url, [data], [callback])

Type: `method` · Since: `0.0.96`

Invia una richiesta virtuale a una route.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Il metodo e il percorso, ad es. `'GET /api/products/'`. |
| `data` | `Object` | no |  | Il body della richiesta. |
| `callback` | `Function(err, response)` | no |  | Chiamata con la risposta. |

Returns: `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.

Example: Richiesta autorizzata
```js
var ctrl = ACTION('+POST /api/orders/', { product: 'A1' }, function(err, response) {
	console.log(err, response);
});
ctrl.user = { id: 'test' };
```

### TEST(fn(add, response), [done])

Type: `method` · Since: `0.0.96`

Esegue un elenco di richieste virtuali e riporta il risultato.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(add, response)` | yes |  | Dichiara le richieste. |
| `done` | `Function(errors, output)|Controller` | no |  | Riceve il risultato. |

Returns: `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 }`.

Example: Una route di test
```js
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);
}
```

### action.data(body)

Type: `method` · Since: `0.0.96`

Imposta il body di una richiesta di test.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `body` | `Object` | yes |  | Il body. |

Returns: `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.

Example: Concatenare
```js
add('+POST /api/orders/', 'Order').data({ product: 'A1' }).user({ id: 'admin', sa: true });
```

### action.pass(fn(res))

Type: `method` · Since: `0.0.96`

Segna la richiesta come fallita a meno che `fn` non restituisca `true`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(res)` | yes |  | Restituisce `true` quando la risposta è corretta. |

Returns: `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.

Example: Verificare il valore
```js
add('GET /api/products/', 'Products').pass(function(res) {
	if (!res.value.length)
		res.invalid('Empty list');
	return res.value.length > 0;
});
```

### action.make(fn(action, response))

Type: `method` · Since: `0.0.96`

Prepara una richiesta di test subito prima che venga eseguita.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(action, response)` | yes |  | Modifica l'azione prima che venga eseguita. |

Returns: `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.

Example: Usare una risposta precedente
```js
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 + '/');
});
```
