Sunday, 30 January 2011

A Guide to Test Driven Development in Flex – Part 3: Implementing the Class

At the end of part 2 we had completed all of our unit tests and most of them were failing:

We can now go ahead and complete the class implementation and make sure all these tests pass. Lets concentrate on the first test we wrote that is failing – popupOpensOnStartSymbol.
This test asserts that when the user types the startSymbol the PM opens the popup. At the moment this test is failing:

To fix this we add the minimum amount of code to the SUT to make this test pass:

We now run the test again and find that it is still failing:

So we add a bit more code to the SUT:

And now when we run the test it passes:

However one of the tests that was previously passing is now failing – popupRemainsClosedNonStartSymbol:

We are now getting the binding event firing even we don’t type the startSymbol. This is easily fixed:

And we can see that popupOpensOnStartSymbol still passes and now popupRemainsClosedNonStartSymbol passes as well.

It is important to fix the tests in this way. It would certainly be possible to just implement that one method in one go and have both tests pass straight away. Had we done that however we would not know if we had written our tests correctly. Having gone through this process we have now verified that if the SUT does not dispatch a binding event the test fails. We have have also verified that if the model dispatches a binding event when it is not supposed to the popupRemainsClosedNonStartSymbol test fails. Implementing the class in this way tests our tests.

Using this process I will implement the rest of the functionality for the model. When complete you end up with this.

That’s the end of my guide to Test Driven Development. It’s not as strict nor has quite as many steps as some of the other methods I have seen but it works for me. I believe that developing in this way produces much better code that is easily maintainable any much less fragile and can be quicker.

If you have any feedback please let me know in the comments.

No comments:

Post a comment