Last Test Driven Development session I refactored the last test I did to make it easier to read and understand. I created a couple of helper functions to do this. Functions that would take specified inputs, update the fields, then test to see if the resulting audit dictionary object was the same as the inputs that were given.
I’m going to take a look at my previous tests and see if I can replace the old helper functions with these new helper functions so everything is using the same structure and everything will appear simpler.
OK, now I’m going to get started.
This was the first test I had made in this iteration of tests:
'@TestMethod("Count Changes")
Private Sub WhenNoFieldIsChangedThenReturnEmptyListOfChanges()
Dim testFormAuditor As New FormAuditor
Dim testCollection As New Collection
Set testCollection = testFormAuditor.ListOfChanges
Assert.AreEqual CLng(0), testCollection.Count
End Sub
The above test doesn’t actually change any fields and it doesn’t have any helper functions… It is fine, moving on to the next test:
'@TestMethod("Count Changes")
Private Sub WhenOneFieldIsChangedThenReturnSingleListOfChanges()
Dim testFormAuditor As FormAuditor
Dim testCollection As New Collection
Set testFormAuditor = New FormAuditor
ChangeTestTextToRandomString
Set testCollection = testFormAuditor.ListOfChanges
Assert.AreEqual CLng(1), testCollection.Count
End Sub
Ok for the above function, here we go, we do have to change a field, so I can use the inputs. I am only counting the resulting dictionary, so I don’t need to use the comparison function. Let’s see what I can do with that:
'@TestMethod("Count Changes")
Private Sub WhenOneFieldIsChangedThenReturnSingleListOfChanges()
Dim TestTextChange As AuditFieldChange
Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
dctInputs.Add "TestText", Array("Hello World", "Goodbye World")
Set colResults = SetFields_ChangeThem_ReturnCollection(dctInputs)
Assert.AreEqual CLng(1), colResults.Count
End Sub
For the above changes, I realized as I started that the new function I wanted to use returned a dictionary and required at least one change to be made. This test counts the number of elements in the collection of events above the dictionary.
Therefore, I had to change the function to return the collection instead. Here’s the updated helper function:
Private Function SetFields_ChangeThem_ReturnCollection(dctFieldName_arrStartAndEndVals As Scripting.Dictionary) As VBA.Collection
Dim testFormAuditor As FormAuditor
ChangeFields dctFieldName_arrStartAndEndVals, 0
Set testFormAuditor = New FormAuditor
ChangeFields dctFieldName_arrStartAndEndVals, 1
Set SetFields_ChangeThem_ReturnCollection = testFormAuditor.ListOfChanges
End Function
This also meant I needed to change the latest test to use the collection instead of the dictionary so I do that as seen here (note that the only difference is in the declarations and then how I call the final assert function):
'@TestMethod("Verify Changes")
Private Sub WhenTwoTextFieldsChangeBeforeAndAfterValuesAreReturned()
Dim TestTextChange As AuditFieldChange, TestComboChange As AuditFieldChange
Dim dctInputs As New Scripting.Dictionary, colResults As VBA.Collection
dctInputs.Add "TestText", Array("TextBeforeValue", "TextAfterValue")
dctInputs.Add "TestCombo", Array("ComboBeforeValue", "ComboAfterValue")
Set colResults = SetFields_ChangeThem_ReturnCollection(dctInputs)
Assert.IsTrue FieldInputsMatchResults(dctInputs, colResults(1).FieldChanges)
End Sub
Even though I also had to further refactor, I think it is pretty good and did shorten the second test “WhenOneFieldIsChangedThenReturnSingleListOfChanges” by one line. And I was able to remove the function that just changed the value to a random new value.
So far so good. I think the next counting tests will be very easy now to refactor.