Coming from a .Net background with strong emphasis on best practices, one of my first missions when learning Flex, was to figure out how I could implement Behavior Driven Development in this environment.
I tried a lot of different approaches and will show three of them in the following posts.
I will not explain what is meant by BDD – there are numerous blogs out there and a good starting point is this wikipedia article.
At this point a simple comparison to TDD (Test Driven Development) shall suffice:
- Arrange in TDD corresponds to a Scenario/Context in BDD and is usually expressed inside a “Context”, “EstablishContext” or a “Given” codeblock
- Act in TDD corresponds to an action done to the system under test and is expressed in BDD inside a “Because” or “When” codeblock
- Assertions in TDD correspond to “should” methods in BDD like shouldEqual, shouldBeTrue etc.
The details of this correlation will become clear in the examples.
Choosing a testing framework
Although there are numerous Flex testing frameworks out there, none of them lend themselves particularly well to BDD.
I gave fluxunit – modeled after Rspec , a testing framework for Ruby – a drive.
It’s architecture would be very BDD enabling, but at this point it is still missing a lot of needed aspects.
For example it presents all test results as panels inside the browser window, marking the failed ones as red, which would require the developer to go hunt for the failures by scrolling through all of them.
That and the fact that no stack traces of the failures are given, make it not mature enough to be used in a serious project at this point.
Unfortunately it seems to be a dead, since it wasn’t updated for over a year.
Other testing frameworks are not offering anything very different to what is offered by FlexUnit4 which additionally has the following advantages:
- it is fully integrated into Adobe’s FlashBuilder and allows running the tests and inspecting the results inside the IDE
- it is supported by Adobe and will most likely be evolved in the future
- a lot of frameworks related to testing (e.g. the mocking framework mockito) integrate with FlexUnit
- there is a lot of good documentation out there like this “getting up and running” tutorial
FlexUnit4 is not designed to be used as a BDD framework though and a few hurdles will need to be overcome.
What we’ll be testing
The source code for this application is available here.
To get started, I implemented a sample application following the traditional TDD style as explained in part 2.
The application I created has 3 components – an employer, an employee and a bank account:
- is either connected or not
- can be updated
- can get fired
- can get paid only if the bank account is connected
- will show if he did good work or not
- handles the Employee
- evaluates the Employee and fires him if he didn’t do any good work
- should only pay the Employee when his salary is greater zero, he wasn’t paid before and was not fired either
I will show three different ways to test the above mentioned classes.
- The traditional way – TDD, using manually created mocks.
- BDD in a way that fully integrates with the FlexUnit4 framework and FlashBuilder and thus can use the automatically created test runner, but requires a lot of classes. We will use manually created mocks here as well.
- BDD in a way that requires us to create a custom test runner, but only two classes for our tests. We will use mockito to supply our mocks. This is in my opinion the most efficient way in order to get things done.
- A look at using mockito in a way that seamlessly integrates with the approach described above.