> For the complete documentation index, see [llms.txt](https://testbox.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://testbox.ortusbooks.com/v2.x-1/in-depth/life-cycle-methods/bdd.md).

# BDD

* `beforeAll()` - Executes once before all specs for the entire test bundle CFC
* `afterAll()` - Executes once after all specs complete in the test bundle CFC
* `run( testResults, TestBox )` - Executes once so it can capture all your describe and it blocks
* `beforeEach( body, data )` - Executes before every single spec in a single describe block and receives the currently executing spec.
* `afterEach( body, data )` - Executes after every single spec in a single describe block and receives the currently executing spec.
* `aroundEach( body, data )` - Executes around the executing spec so you can provide code surrouding the spec.

```javascript
component{
     function beforeAll(){}
     function afterAll(){}

     function run( testResults, testBox ){
          describe("A Spec", function(){

               beforeEach( function( currentSpec ){
                    // before each spec in this suite
               });

               afterEach( function( currentSpec ){
                    // after each spec in this suite
               });

               aroundEach( function( spec, suite ){
                    // execute the spec
                    arguments.spec.body();
               });

               describe("A nested suite", function(){

                    // my parent's aroundEach()

                    beforeEach( function(){
                         // before each spec in this suite + my parent's beforeEach()
                    });

                    afterEach( function(){
                         // after each spec in this suite + my parent's afterEach()
                    });

                });

          });

          describe("A second spec", function(){

               beforeEach( function( currentSpec ){
                    // before each spec in this suite, separate from the two other ones
               });

               afterEach( function( currentSpec ){
                    // after each spec in this suite, separate from the two other ones
               });

          });
     }
}
```

The great flexibility of the BDD approach is that it allows you to nest `describe` blocks or create multiple `describe` blocks. Each `describe` block can have its own life-cycle methods as well. Not only that, if they are nested, TestBox will walk the tree and call each `beforeEach()` and `afterEach()` in the order you declare them.

## Life-Cycle Data Binding

You can pass in an argument called `data` which is a `struct` of dynamic data to pass into the life-cycle method. You can then pickup this data in the closure for the life-cycle.

```
beforeEach( 
    data = { mydata="luis" }, 
    body = function( currentSpec, data ){
        // The arguments.data is binded via the `data` snapshot above.
        data.myData == "luis";
    }
);
```

Here is a typical example:

```javascript
describe( "Ability to bind data to life-cycle methods", function(){

    var data = [
        "spec1",
        "spec2"
    ];

    for( var thisData in data ){
        describe( "Trying #thisData#", function(){

            beforeEach( data={ myData = thisData }, body=function( currentSpec, data ){
                targetData = arguments.data.myData;
            });

            it( title="should account for life-cycle data binding", 
                data={ myData = thisData},
                body=function( data ){
                    expect(    targetData ).toBe( data.mydata );
                }
            );

            afterEach( data={ myData = thisData }, body=function( currentSpec, data ){
                targetData = arguments.data.myData;
            });
        });
    }

    for( var thisData in data ){

        describe( "Trying around life-cycles with #thisData#", function(){

            aroundEach( data={ myData = thisData }, body = function( spec, suite, data ){
                targetData = arguments.data.myData;
                arguments.spec.body( data=arguments.spec.data );
            });

            it( title="should account for life-cycle data binding", 
                data={ myData = thisData },
                body=function( data ){
                    expect(    targetData ).toBe( data.mydata );
                }
            );

        });

    }
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://testbox.ortusbooks.com/v2.x-1/in-depth/life-cycle-methods/bdd.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
