Quick question to my readers: If you know a good way of highlighting the code in an email I’d like to do that. I notice that my code in the email is not marking up properly whereas it looks better on the site itself. I’m using a WordPress feed and the Prismatic plugin when I write the post. Has anyone already done this?
Yesterday I finished refactoring my support function. I am now going to look at the test itself to see if I can make it a little prettier, more readable, and more self documenting. Here it is right now:
'@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(dctInputs)
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
Blech. When compared with my nice new refactored functions from yesterday, this definitely needs a makeover.
I think if I have a function to create the dictionary with named parameters… Hmm… well, need to consider that I have multiple items I want to feed to this dictionary. Kinda screaming for a new class if I want to do it with better names and such. How about I reuse the actual objects from the program side. I can still use AuditFieldChange objects, but wrap them into a function to create them. I’ll move my Dim statements for everything to the top. I can remove the “New” keywords since I’m already setting the variables to an existing element later.
'@TestMethod("Verify Changes")
Private Sub WhenTwoTextFieldsChangeBeforeAndAfterValuesAreReturned()
Dim TestTextChange As AuditFieldChange, TestComboChange As AuditFieldChange
Dim dctInputs As New Scripting.Dictionary, dctResults As Scripting.Dictionary
dctInputs.Add "TestText", Array("TextBeforeValue", "TextAfterValue")
dctInputs.Add "TestCombo", Array("ComboBeforeValue", "ComboAfterValue")
Set dctResults = SetFields_ChangeThem_ReturnDictionary(dctInputs)
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
Ok, that’s down to 8 lines although I don’t know if I like all the Dims at the top. I’ll think about that later. Right now, I just really want to take care of that giant long rambling Assert line at the end. How can I make that more compact?
I started like this, but it’s not helping. However the first boolean test looks like something I could reduce into a function with a much smaller name:
With dctResults
Assert.IsTrue .Item("TestText").OldValue = dctInputs("TestText")(0) And TestTextChange.NewValue = "TextAfterValue" And TestComboChange.OldValue = "ComboBeforeValue" And TestComboChange.NewValue = "ComboAfterValue"
End With
I moved on to this on the With line using a new helper function (once done, wouldn’t need the With statement). BUT, it’s still too long having to call 3 arguments.
...
With dctResults
Assert.IsTrue FieldBeforeIsSame("TestText", dctInputs, dctResults) And TestTextChange.NewValue = "TextAfterValue" And TestComboChange.OldValue = "ComboBeforeValue" And TestComboChange.NewValue = "ComboAfterValue"
End With
End Sub
Private Function FieldBeforeIsSame(FieldName As String, dctInputs As Scripting.Dictionary, dctResults As Scripting.Dictionary) As Boolean
Dim retVal As Boolean
retVal = (dctResults(FieldName).OldValue = dctInputs(FieldName)(0))
FieldBeforeIsSame = retVal
End Function
So, I think I could just loop through the input dictionary and check the results dictionary in just one function call and that would be more succinct in the test. Looks like I’m out of time today though, so I’ll save it for tomorrow!