Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

Our test driven development continues!

We are creating a Form Auditor and so far have a basic form listening class and now we need a class which will iterate over a form and collect the Old and New values of fields that are different. I am creating a skeleton of the FormIterator class now. Currently we are at a failing test for the Setup method of the FormIterator.

This will be similar to the FormListener setup in that we need to have a form that we’ll be looking at. I think we’ll be setting up this iterator class once in the FormListener and then just use it over and over to collect the data. I’m almost starting to see another object which maybe will actually be the FormAuditor main class eventually. Let’s see where it goes.

I’m adding a Private frm object but this time, the iterator doesn’t need to hook into the form events. That’s the listeners job so we will not use the “WithEvents” keyword this time. Oh, wait, actually I’m not going to add that yet, it’s not necessary for the Setup method to run. So I’m just creating the Setup stub for now to get the test to pass:

Public Sub Setup(theForm As Access.Form)
End Sub

This causes the test to pass so we are good there. Didn’t do much, so no refactoring needed. Now to write another failing test. In the case of the iterator, I want it go over all the fields of the form and pull the prior values of the fields and look at the new values of the fields and return the fields that have changed. So maybe I will start by creating an iterate method. That will be my next test.

So far here is my new test:

'@TestMethod("FormIterator")
Private Sub IterateWhenNothingChangedReturnsEmptyDictionary()
    Dim FormIteratorTest As New FormIterator
    Dim testDictionary As Scripting.Dictionary
    FormIteratorTest.Setup NewForm
    Set testDictionary = FormIteratorTest.Iterate()
    
End Sub

So now I’m faced with how to write the RubberDuck Assert. The Assert object has “AreEqual” but that is comparing two scalar values I think. I see a couple other possible methods so I’m going to refer to the documentation.

Hmm.. Here is a link to the unit testing section: Unit Testing | rubberduckvba.com. However, their API that pulls in information about specific methods is reporting it’s broken!

After some creative Bing-ing. I found this alternative documentation on their GitHub project for RubberDuckVBA: Unit Testing · rubberduck-vba/Rubberduck Wiki · GitHub

This gives a nice little table with descriptions for all the Assert methods:

NameDescription
AreEqualVerifies that two specified objects are equal. The assertion fails if the objects are not equal.
AreNotEqualVerifies that two specified objects are not equal. The assertion fails if the objects are equal.
AreNotSameVerifies that two specified object variables refer to different objects. The assertion fails if they refer to the same object.
AreSameVerifies that two specified object variables refer to the same object. The assertion fails if they refer to different objects.
FailFails the assertion without checking any conditions.
InconclusiveIndicates that the assertion cannot be verified.
IsFalseVerifies that the specified condition is false. The assertion fails if the condition is true.
IsNothingVerifies that the specified object is Nothing. The assertion fails if it is not Nothing.
IsNotNothingVerifies that the specified object is not Nothing. The assertion fails if it is Nothing.
IsTrueVerifies that the specified condition is true. The assertion fails if the condition is false.
NotSequenceEqualsVerifies that at least one of the items in 2 arrays(2D) differs at any give index. The assertion fails if all of the items are the same, if the lower bounds and upper bounds are the same, and the ranks (number of dimensions) are the same.
SequenceEqualsVerifies that all of the items in 2 arrays(2D) are equal. The assertion fails if any items is different, if either the lower bounds or upper bounds are different, or if the ranks (number of dimensions) differ.
SucceedPasses the assertion without checking any conditions.
Definitions as reviewed on 12/15/2023

So perhaps I will want to try SequenceEquals. Even though it’s a dictonary, perhaps it will handle it.

Alternately, since it is a dictionary and I am expecting it to be empty, I could just count the elements in the returned dictionary. There should be zero elements. That sounds much easier. However, I have now run out of time so it will have to wait until my next TDD article!