Now our auditor will need to be a little smarter. We need to check the beginning and ending value of the test text field during the test in the form Before Update event. We won’t yet consider canceled BeforeUpdate events, I’ll just keep that in the back of my mind for later. Right now we have a failing test that needs to pass.
Here is the Test code again:
'@TestMethod("Count Changes")
Private Sub WhenOneFieldIsChangedToSameValueThenReturnNoListOfChanges()
Dim testFormAuditor As FormAuditor
Dim testCollection As New Collection
Set testFormAuditor = New FormAuditor
ChangeTestText NewForm.TestText
Set testCollection = testFormAuditor.ListOfChanges
Assert.AreEqual CLng(0), testCollection.Count
End Sub
The test is just setting the TextBox string back to itself.
Here is our FormAuditor class so far:
Option Compare Database
Option Explicit
Private WithEvents FormToAudit As Access.Form
Private pListOfChanges As New Collection
Public Property Get ListOfChanges() As Collection
Dim retVal As Collection
Set retVal = pListOfChanges
Set ListOfChanges = retVal
End Property
Private Sub Class_Initialize()
Set FormToAudit = Forms("TestForm")
FormToAudit.BeforeUpdate = "[Event Procedure]"
End Sub
Private Sub FormToAudit_BeforeUpdate(Cancel As Integer)
pListOfChanges.Add FormToAudit.TestText.Value
End Sub
So it looks like I could do the check in the BeforeUpdate event at the bottom like this:
Private Sub FormToAudit_BeforeUpdate(Cancel As Integer)
If FormToAudit.TestText.OldValue = FormToAudit.TestText.Value Then
pListOfChanges.Add FormToAudit.TestText.Value
End If
End Sub
I think in this case this will pass the test. Let’s see!
Uh oh. I now have 3 broken tests. Looking at the expected results vs the actual, they look backwards. Looks like I reversed the condition I wanted and only am adding to the list of changes when the old value and new value are the SAME! Well that’s easy to fix:
Private Sub FormToAudit_BeforeUpdate(Cancel As Integer)
If FormToAudit.TestText.OldValue <> FormToAudit.TestText.Value Then
pListOfChanges.Add FormToAudit.TestText.Value
End If
End Sub
That’s better, I bet it will work now:
There we go. Now I’ll forgo refactoring for now and see if I can think up another requirement I want to prove is currently failing. I think my comparison is somewhat weak. Let’s test the null values cases next.
I just played around a little with the Form and null using the Intermediate window.
It seems doing a If Null = “” condition does not fail but returns false.
My little function ChangeTestText though will not accept a Null, so the test itself would fail if I tried to set the control value to Null using the function. Do I want the Auditor to make sure that if the control is empty with Null or empty with a 0 length string it treats them the same? Or do I want the Auditor to do strict typing? Hmmm… sounds like a question for another day.