L'uso di un modulo può rendere più facile avere un setup/smontaggio globale per la tua suite di test. Ecco un esempio utilizzando RequireJS (moduli AMD):
In primo luogo, definiamo un ambiente di test con il nostro globale setup/teardown:
// test-env.js
define('test-env', [], function() {
// One can store globals, which will be available within the
// whole test suite.
var my_global = true;
before(function() {
// global setup
});
return after(function() {
// global teardown
});
});
Nel nostro corridore JS (inclusa nel corridore HTML di moka, lungo l'altra librerie e file di test, come <script type="text/javascript">…</script>
, o meglio, come un file JS esterno):
require([
// this is the important thing: require the test-env dependency first
'test-env',
// then, require the specs
'some-test-file'
], function() {
mocha.run();
});
some-test-file.js
potrebbero essere attuate in questo modo:
// some-test-file.js
define(['unit-under-test'], function(UnitUnderTest) {
return describe('Some unit under test', function() {
before(function() {
// locally "global" setup
});
beforeEach(function() {
});
afterEach(function() {
});
after(function() {
// locally "global" teardown
});
it('exists', function() {
// let's specify the unit under test
});
});
});
puoi spiegare un po 'cosa sta succedendo lì? – Gobliins