Today I will refactor the remaining test from the Count group which is counting returned times the BeforeUpdate was triggered. Here is the test I am working to refactor:
'@TestMethod("Count Changes")
Private Sub WhenTwoFieldsAreChangedThenReturnTwoEntryListOfChanges()
Dim testFormAuditor As FormAuditor
Dim testCollection As Collection
Set testFormAuditor = New FormAuditor
ChangeTestTextToRandomString
ChangeTestTextToRandomString
Set testCollection = testFormAuditor.ListOfChanges
Assert.AreEqual CLng(2), testCollection.Count
End Sub
I had already started to break the function: “SetFields_ChangeThem_ReturnCollection” into 2 separate functions and here’s what I have so far:
Private Function ChangeFields_ReturnExistingListOfChanges(fa As FormAuditor, dctFieldName_arrStartAndEndVals As Scripting.Dictionary) As VBA.Collection
ChangeFields dctFieldName_arrStartAndEndVals, 1
Set ChangeFields_ReturnExistingListOfChanges = testFormAuditor.ListOfChanges
End Function
Private Function SetFields_ChangeThem_ReturnNewListOfChanges(dctFieldName_arrStartAndEndVals As Scripting.Dictionary) As VBA.Collection
Dim testFormAuditor As FormAuditor
ChangeFields dctFieldName_arrStartAndEndVals, 0
Set testFormAuditor = New FormAuditor
ChangeFields_ReturnExistingListOfChanges testFormAuditor, dctFieldName_arrStartAndEndVals
Set SetFields_ChangeThem_ReturnNewListOfChanges = testFormAuditor.ListOfChanges
End Function
I think this refactoring into 2 functions might have been enough to finish off my refactoring of the test to at least start using them. What I did was refactor the original function so it will use a new function “ChangeFields_ReturnExistingListOfChanges” which will take the FormAuditor as an object. So here is my attempt. I think it might be kind of clunky but here goes:
'@TestMethod("Count Changes")
Private Sub WhenTwoFieldsAreChangedThenReturnTwoEntryListOfChanges()
Dim testFormAuditor As FormAuditor, dctInput As New Scripting.Dictionary
Set testFormAuditor = New FormAuditor
dctInput.Add "TestText", Array("", CStr(Now()))
ChangeFields_ReturnExistingListOfChanges testFormAuditor, dctInput
Set dctInput = New Scripting.Dictionary
dctInput.Add "TestText", Array("", CStr(Now() & " also"))
ChangeFields_ReturnExistingListOfChanges testFormAuditor, dctInput
Assert.AreEqual CLng(2), testFormAuditor.ListOfChanges.Count
End Sub
Ok, so that works and passes the test, HOWEVER, it is very unreadable in my opinion. Since that is the case, I will look at making it more readable tomorrow as there will be other tests of this kind.