Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

As I continue to refactor the test, this time I am going to try to create the dictionary object I think I want to get back and pass that to a helper function which will change the fields to the before state, instantiate the FormAuditor class, run a single BeforeUpdate event setting the fields to the after state, and get the resulting dictionary back from the helper function and make sure it has the same before and after field values I’m expecting.

Here’s where I started:

'@TestMethod("Verify Changes")
Private Sub WhenTwoTextFieldsChangeBeforeAndAfterValuesAreReturned()
    Dim testCollection As New Collection
    Dim TestTextChange As New AuditFieldChange, TestComboChange As New AuditFieldChange
    Set testCollection = SetFields_ChangeThem_ReturnListOfChanges()
    Set TestTextChange = testCollection.Item(1).FieldChanges("TestText")
    Set TestComboChange = testCollection.Item(1).FieldChanges("TestCombo")
    Assert.IsTrue TestTextChange.OldValue = "TextBeforeValue" And TestTextChange.NewValue = "TextAfterValue" And TestComboChange.OldValue = "ComboBeforeValue" And TestComboChange.NewValue = "ComboAfterValue"
End Sub

So I changed testCollection to dctResults as a dictionary instead of a collection. I did this both in the Dim statement and then changed the instances in the code to the results dictionary. I also changed the helper function return type and the name of the function to remove the ReturnListOfChanges and change that to ReturnDictionary for consistency. I changed the necessary parts of the function so that this would continue to compile.

'@TestMethod("Verify Changes")
Private Sub WhenTwoTextFieldsChangeBeforeAndAfterValuesAreReturned()
    Dim dctInputs As Scripting.Dictionary
    Dim dctResults As Scripting.Dictionary
    Dim TestTextChange As New AuditFieldChange, TestComboChange As New AuditFieldChange
    Set dctResults = SetFields_ChangeThem_ReturnDictionary()
    Set TestTextChange = dctResults("TestText")
    Set TestComboChange = dctResults("TestCombo")
    Assert.IsTrue TestTextChange.OldValue = "TextBeforeValue" And TestTextChange.NewValue = "TextAfterValue" And TestComboChange.OldValue = "ComboBeforeValue" And TestComboChange.NewValue = "ComboAfterValue"
End Sub

Now I’ll create the dctInputs values I want. Maybe I’ll just leave it an array of oldvalue, newvalue to start. Notice at the top of the next function I’m just decalring the variable dctInputs as a new dictionary then I’m adding the two fields I want to change by field name with an array for the before value and new value. I still don’t think this is quite explicit enough. I think I will end up creating another function and using named arguments to make it even more self documenting, but here’s what I have so far:

'@TestMethod("Verify Changes")
Private Sub WhenTwoTextFieldsChangeBeforeAndAfterValuesAreReturned()
    Dim dctInputs As New Scripting.Dictionary
    dctInputs.Add "TestText", Array("TextBeforeValue", "TextAfterValue")
    dctInputs.Add "TestCombo", Array("ComboBeforeValue", "ComboAfterValue")
    Dim dctResults As Scripting.Dictionary
    Dim TestTextChange As New AuditFieldChange, TestComboChange As New AuditFieldChange
    Set dctResults = SetFields_ChangeThem_ReturnDictionary()
    Set TestTextChange = dctResults("TestText")
    Set TestComboChange = dctResults("TestCombo")
    Assert.IsTrue TestTextChange.OldValue = "TextBeforeValue" And TestTextChange.NewValue = "TextAfterValue" And TestComboChange.OldValue = "ComboBeforeValue" And TestComboChange.NewValue = "ComboAfterValue"
End Sub

At this point I’m working on the supporting function. I’ve started changing the parameter value the function accepts by name and type, then started looping over it and changed the loop variable from arr indicating an array to itm indicating an item.

Private Function SetFields_ChangeThem_ReturnDictionary(dctFieldName_arrStartAndEndVals As Dictionary) As Dictionary
    Dim testFormAuditor As FormAuditor
    Dim itm As Variant
    For Each itm In dctFieldName_arrStartAndEndVals
        ChangeFields (itm(0))
    Next itm
    ChangeFields Array("TestText", "TextBeforeValue"), Array("TestCombo", "ComboBeforeValue")
    Set testFormAuditor = New FormAuditor
    ChangeFields Array("TestText", "TextAfterValue"), Array("TestCombo", "ComboAfterValue")
    
End Function

We are left in a compilable state again which is halfway to where I want to be when refactoring. But the tests still fail and are broken. Next time I will work on getting the support function further towards completion.