Sunday, June 11, 2017

True Unit tests in Bold ?

I think you all heard about unit testing. Maybe some of you even practise it?

I recently bought Dependency Injection book by Nick Hodges. It is about to have loose coupled classes. And this is a precondition for true unit-test where each class is tested independently.

This make me thinking. How would that be possible in Bold? An important part in Dependency Injection is interfaces but the model don't support that now. Ex class TPerson have a link to TAdress. There may also be a method like TAdress.AddPerson(aPerson: TPerson).

To test AddPerson with a unit-test one interface is needed IPerson. The signature need to be changed to TAdress.AddPerson(aPerson: IPerson). Now the interface can be used instead. The same is true for any relations between classes. If there is a single link TPerson.homeAdress: TAdress that should be changed to TPerson.homeAdress: IAdress.

Our main Application Attracs have a huge model. Over 400 classes. So to change this some kind of automation is needed. I thought about to scan businessclasses.pas with a program and generate a new file businessclasses_Interfacedef.pas to differentiate with the original businessclasses_interface.pas.

The new file would contain interfaces to classes in businessclasses_interface.pas like IPerson, IAdress etc. The public methods in each class should be added manually as you cannot indtroduce a lot of interfaces in am application in one step. Easier to divide it in smaller steps. So the generation of file businessclasses_Interfacedef.pas preserve existing methods. New interfaces has no methods.

Next step would be to change application code to use interfaces.

Instead of

vPerson.homeAddress as TAddress;


vPerson.homeAddress as IAddress;

And I think this is the main issue, a lot of changes is required.
But it can at least be done in smaller steps, class by class.

And the rewards is possibility to use true unit-testing. Test all methods in a class. As interfaces is used in relations and parameters fake instances can be used.

Comments are welcome because all this is completely untested. I just brainstorm here :)

No comments:

Post a Comment