I thought I would update folks on my TDD progress using VBA in Access.
I use the RubberDuckVBA Unit Testing component to create and run tests. You don’t need a fancy program or system like that to utilize automated testing, but I have enjoyed using it and the nice graphical green checkbox or red x showing me whether my test procedure passes or fails and it is easy for me to use it.
The main difference in my coding when I test is that I look for ways to code that will make it easier to build tests that will be able to test reusable code for the test and the code behind a form or report.
This means I have tended to use simple means to mimic Form fields for example so that I could create a mockup of the data arrangement on the form without actually loading the form and running all the code to get to the point where I can test the thing I want to test. This is so that my tests can be small subroutines with minimal setup and run very quickly.
Practically, so far I have used simple dictionaries to mimic Forms to return values, and I have a form field getter class that is used for both the Form and the Test for code that is under test. This allows me to use the exact same production code whether I am running the test or running the form. This is critical for doing TDD.
This breaks the nice feature though of being able to use autocomplete when writing my production code.
Instead of using Me.Quantity for example, I replace the function to use GetVal(“Quantity”) where the GetVal function uses a scripting dictionary for the test, but the function uses something like Me(varName) to get the variable from the form. Inside the GetVal function, it looks to see if the “getter” object is initialized and if not, creates the getter that uses the actual form, but if it is, then the test has created the getter using the dictionary for the test.
I am thinking that a way to restore the autocomplete issue would be to build a class that had all the form fields on it and that class could specifically define those fields in the class thereby restoring autocomplete. It could then use the dictionary or form retrieval methods I am already using.
There is some code that was demonstrated in an Access User Group presentation that built a class based on the fields on a form. I believe I have the code sitting on my hard drive somewhere. I tried to find the presentation, but without watching several of them, I wasn’t sure which it was, but I thought John Colby was the author of the original code. It was only available if you joined the Access User Groups web site and the particular group he did the presentation for.
Anyway, this technique might allow me to get even more sophisticated with my testing, although I wanted it to be as simple as possible. Some forms though with lots of complexity and fields could definitely benefit from having autocomplete in the VBA as well as allowing the compiler to catch errors. After updating the form there could be an automated test to make sure the class and form fields match and it could be trivial to run a function to update the classes if needed.