This is my very-first-ever run at TDD. How did I do?
In keeping with Uncle Bob's Three Rules, I wrote every test first, starting in order from the top down as you see here (below). Here's the sequence I used:
- For each new method in Production I entered the method-name-to-be in Test.
- I received a compile-time error.
- I used Visual Studio's error correction feature to generate the method stubs.
- I built the solution and ran all tests, resulting in a failure on the most recent test.
- I wrote Production code to cause the failed test to pass.
- I built the solution and ran all tests, verifying that they all passed.
To tell you the truth, this all seems pretty redundant. As you can see, each of my tests contains code copied from previous tests--I had to do that so the test would run. I tried making functions out of them, so I could chain them together, but that caused Visual Studio to not see the tests.
Certainly there must be a better way. I'd love to know what it is.
Production:
Public Module Main
Sub Main()
End Sub
Function GetSessionType(RemoteHostName As String) As Type
Return Type.GetTypeFromProgID("Microsoft.Update.Session", RemoteHostName)
End Function
Function GetSession(Type As Type) As UpdateSession
Return Activator.CreateInstance(Type)
End Function
Function GetSearcher(Session As UpdateSession) As IUpdateSearcher
Return Session.CreateUpdateSearcher
End Function
Function GetHistoryCount(Searcher As IUpdateSearcher) As Integer
Return Searcher.GetTotalHistoryCount
End Function
Function GetHistory(Searcher As IUpdateSearcher, Count As Integer) As IUpdateHistoryEntryCollection
Return Searcher.QueryHistory(0, Count)
End Function
Function EnumerateHistory(History As IUpdateHistoryEntryCollection, Count As Integer) As Boolean
Dim oEntry As IUpdateHistoryEntry
EnumerateHistory = True
For iIndex = 0 To Count - 1
Try
oEntry = History(iIndex)
Console.WriteLine("Title: {0}{2}Date: {1}", oEntry.Title, oEntry.Date, vbTab)
Catch ex As Runtime.InteropServices.COMException
' For some reason on MyComputer the GetTotalHistoryCount() method '
' call returns a count higher than the existing update count. This '
' catch bypasses the resulting IndexOutOfRange COM errors. '
Catch ex As Exception
EnumerateHistory = False
End Try
Next
End Function
End Module
Test:
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting
<TestClass()>
Public Class UpdateHistoryReaderTests
Private Const REMOTE_HOST_NAME As String = "MyComputer"
<TestMethod()>
Public Sub GetSessionTypeTest()
Dim oType As Type
oType = UpdateHistoryReader.Main.GetSessionType(REMOTE_HOST_NAME)
Assert.IsNotNull(oType)
End Sub
<TestMethod()>
Public Sub GetSessionTest()
Dim oSession As UpdateSession
Dim oType As Type
oType = UpdateHistoryReader.Main.GetSessionType(REMOTE_HOST_NAME)
oSession = UpdateHistoryReader.Main.GetSession(oType)
Assert.IsNotNull(oSession)
End Sub
<TestMethod()>
Public Sub GetSearcherTest()
Dim oSearcher As IUpdateSearcher
Dim oSession As UpdateSession
Dim oType As Type
oType = UpdateHistoryReader.Main.GetSessionType(REMOTE_HOST_NAME)
oSession = UpdateHistoryReader.Main.GetSession(oType)
oSearcher = UpdateHistoryReader.Main.GetSearcher(oSession)
Assert.IsNotNull(oSearcher)
End Sub
<TestMethod()>
Public Sub GetHistoryCountTest()
Dim oSearcher As IUpdateSearcher
Dim oSession As UpdateSession
Dim iCount As Integer
Dim oType As Type
oType = UpdateHistoryReader.Main.GetSessionType(REMOTE_HOST_NAME)
oSession = UpdateHistoryReader.Main.GetSession(oType)
oSearcher = UpdateHistoryReader.Main.GetSearcher(oSession)
iCount = UpdateHistoryReader.Main.GetHistoryCount(oSearcher)
Assert.IsFalse(iCount = 0)
End Sub
<TestMethod()>
Public Sub GetHistoryTest()
Dim oSearcher As IUpdateSearcher
Dim oHistory As IUpdateHistoryEntryCollection
Dim oSession As UpdateSession
Dim iCount As Integer
Dim oType As Type
oType = UpdateHistoryReader.Main.GetSessionType(REMOTE_HOST_NAME)
oSession = UpdateHistoryReader.Main.GetSession(oType)
oSearcher = UpdateHistoryReader.Main.GetSearcher(oSession)
iCount = UpdateHistoryReader.Main.GetHistoryCount(oSearcher)
oHistory = UpdateHistoryReader.Main.GetHistory(oSearcher, iCount)
Assert.IsNotNull(oHistory)
End Sub
<TestMethod()>
Public Sub EnumerateHistoryTest()
Dim oSearcher As IUpdateSearcher
Dim oHistory As IUpdateHistoryEntryCollection
Dim oSession As UpdateSession
Dim lSuccess As Boolean
Dim iCount As Integer
Dim oType As Type
oType = UpdateHistoryReader.Main.GetSessionType(REMOTE_HOST_NAME)
oSession = UpdateHistoryReader.Main.GetSession(oType)
oSearcher = UpdateHistoryReader.Main.GetSearcher(oSession)
iCount = UpdateHistoryReader.Main.GetHistoryCount(oSearcher)
oHistory = UpdateHistoryReader.Main.GetHistory(oSearcher, iCount)
lSuccess = UpdateHistoryReader.Main.EnumerateHistory(oHistory, iCount)
Assert.IsTrue(lSuccess)
End Sub
End Class