Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

With the new set of functions that I’ve refactored last time, I’m going to update the other functions to use them. So let’s go back to the first 3 counting tests and use the dictionary creation function to make them more readable.

Here they are now:

'@TestMethod("Count Changes")
Private Sub WhenOneFieldIsChangedThenReturnSingleListOfChanges()
    Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
    dctInputs.Add "TestText", Array("Hello World", "Goodbye World")
    Set colResults = SetFields_ChangeThem_ReturnNewListOfChanges(dctInputs)
    Assert.AreEqual CLng(1), colResults.Count
End Sub

'@TestMethod("Count Changes")
Private Sub WhenOneFieldIsChangedToSameValueThenReturnNoListOfChanges()
    Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
    dctInputs.Add "TestText", Array("Hello World", "Hello World")
    Set colResults = SetFields_ChangeThem_ReturnNewListOfChanges(dctInputs)
    Assert.AreEqual CLng(0), colResults.Count
End Sub

'@TestMethod("Count Changes")
Private Sub WhenFieldChangesFromNullToEmptyStringThenReturnOneEntryListOfChanges()
    Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
    dctInputs.Add "TestText", Array(Null, "")
    Set colResults = SetFields_ChangeThem_ReturnNewListOfChanges(dctInputs)
    Assert.AreEqual CLng(1), colResults.Count
End Sub

The change won’t result in less lines, but it will result in the assignment of the dictionary being obvious about the arguments purposes:

'@TestMethod("Count Changes")
Private Sub WhenOneFieldIsChangedThenReturnSingleListOfChanges()
    Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
    Set dctInputs = CreateAndAddToInputDict(FieldToChange:="TestText", InitialValue:="Hello World", ChangeTo:="Goodbye World")
    Set colResults = SetFields_ChangeThem_ReturnNewListOfChanges(dctInputs)
    Assert.AreEqual CLng(1), colResults.Count
End Sub

'@TestMethod("Count Changes")
Private Sub WhenOneFieldIsChangedToSameValueThenReturnNoListOfChanges()
    Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
    Set dctInputs = CreateAndAddToInputDict(FieldToChange:="TestText", InitialValue:="Hello World", ChangeTo:="Hello World")
    Set colResults = SetFields_ChangeThem_ReturnNewListOfChanges(dctInputs)
    Assert.AreEqual CLng(0), colResults.Count
End Sub

'@TestMethod("Count Changes")
Private Sub WhenFieldChangesFromNullToEmptyStringThenReturnOneEntryListOfChanges()
    Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
    Set dctInputs = CreateAndAddToInputDict(FieldToChange:="TestText", InitialValue:=Null, ChangeTo:="")
    Set colResults = SetFields_ChangeThem_ReturnNewListOfChanges(dctInputs)
    Assert.AreEqual CLng(1), colResults.Count
End Sub

I had to update the helper functions to support passing null values by changing the variable types to Variant and defaulting them to Null.

Now let’s take the other Verify Changes test and refactor that to use the new functions. Here it is before:

'@TestMethod("Verify Changes")
Private Sub WhenTextFieldChangesBeforeAndAfterValuesAreReturned()
    Dim testFormAuditor As FormAuditor
    Dim testCollection As New Collection
    ChangeTestText "BeforeValue"
    Set testFormAuditor = New FormAuditor
    ChangeTestText "AfterValue"
    Set testCollection = testFormAuditor.ListOfChanges
    With testCollection.Item(1).FieldChanges("TestText")
    Assert.IsTrue .OldValue = "BeforeValue" And .NewValue = "AfterValue"
    End With
End Sub

And the resulting refactor is:

'@TestMethod("Verify Changes")
Private Sub WhenTextFieldChangesBeforeAndAfterValuesAreReturned()
    Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
    Set dctInputs = CreateAndAddToInputDict(FieldToChange:="TestText", InitialValue:="BeforeValue", ChangeTo:="AfterValue")
    Set colResults = SetFields_ChangeThem_ReturnNewListOfChanges(dctInputs)
    Assert.IsTrue FieldInputsMatchResults(dctInputs, colResults(1).FieldChanges)
End Sub

All right, not too shabby. Took a little longer than I thought, but now to write some new tests for different types of fields to test their values should be pretty easy. We’ll start on that in the next session.

Before I finish though, I’d just like to point out that I have been using a feature of VBA that allows you to pass parameters to functions by specifying the parameter by the name used in the function. As you can see in the above code, you just enter the parameter name with a := and then the value you want to pass. In this way, it makes it very clear in the code what the function parameters are doing. I made both the InitialValue and ChangeTo parameters optional so that we don’t have to pass dummy values when we just want either an initial value only or a change to value only.

See you soon…