When I took the Coldbox Zero to Hero course, we set up a BaseIntegrationSpec.cfc that I have used for my BDD tests on many different projects over the last few years. In the class, we added a method called wrapInTransaction()
that looks like this:
/**
* This function is tagged as an around each handler. All the integration tests we build
* will be automatically rolled backed. No database corruption
*
* @aroundEach
*/
function wrapInTransaction( spec ) {
transaction action="begin" {
try {
arguments.spec.body();
} catch ( any e ){
rethrow;
} finally {
transaction action="rollback";
}
}
}
One thing that has always baffled me is how TestBox knows to call this method around each it()
spec. How does TestBox know to treat wrapInTransaction()
as an aroundEach()
implicit method?
Follow Up Question: Is there something equivalent to aroundEach()
that wraps completely around either the run()
or describe()
methods? It might be handy to wrap an entire describe()
in a transaction - especially for complex or time-consuming database seeding operations.