Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

I decided to do some tests today, but didn’t even get to writing any yet.

The issue was that I had a function tied to a button click event event inside a form which referenced various controls on the form.

Normally, it’s easy to refactor this by simply putting a reference to the form object as an argument to the function and then using that. However, with TDD, I need to uncouple the form controls from the test because I don’t want to deal with opening the form and getting it into the correct state to run the test. In my opinion, that would add way too much overhead to run the test and I want tests that run fast.

So, how did I decide to decouple the Form from the function?

First I copied the function into a new module. Then I used the VBA Debug->Compile function to start walking through where the function was failing. A compile will fail when the function references the form, an undeclared variable, which now includes any global variables declared outside the function. It also fails on function calls to other functions inside the form.

As I went through the Debug->Compile errors, I built a Public Type in the module to hold the various values I was pulling from form controls.

Something like:

Public Type ExcelReportOptions
  startDate As String
  endDate As String
  familyFilter As String
End Type  

Then in my new module function call I can do this:

Public Function ExportExcelReport(Options As ExcelReportOptions)

Now I can just replace the Me dot field name items with Options dot field name.

I can create this ExcelReportOptions variable in an external test and populate it with the values I am testing. This has decoupled the function from the Form object completely so I can easily run tests without having to instantiate the form. And in the Form event code, I can create a variable of type ExportExcelReport and populate it with the items from the form and pass that to the function. Hence, I am assuring that the test is actually testing in the same way the form will be running the function.