Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

See yesterday’s message in the archive here:

Writing tests for legacy code – Part 1 | Access JumpStart

I’m going to start breaking down the test I wrote and what I’m doing and why. Here is the whole test:

'@TestMethod("DeleteAction")
Private Sub GivenConcreteGroupDeleteTriggered_WhenReadOnlyStatusType_RequirePassword()
    On Error GoTo TestFail
    
    'Arrange:
    Dim LineController As ECI_POLineController
    Set LineController = New ECI_POLineController
    Dim testDictionary As New Scripting.Dictionary
    testDictionary.Add "Cost_Type_ID", "2"
    testDictionary.Add "Product_Type_ID", "1"
    testDictionary.Add "Qty_Ordered", "10"
    testDictionary.Add "Phase_Number", "10"
    testDictionary.Add "Item_Description", "Test Description"
    testDictionary.Add "Qty_Used", "10"
    testDictionary.Add "Unit_of_Measure", "EA"
    testDictionary.Add "Taxable", "True"
    testDictionary.Add "Concrete_Minutes_Apart", "15"
    testDictionary.Add "Line_Status", STATUS_OPEN
    
    'Act:
    Dim frmGet As New FormValueGetter_Test
    Dim PwCount As New T_PasswordsAlwaysTrueCounter
    Set frmGet.FieldValues = testDictionary
    Set LineController.frmGet = frmGet
    Set LineController.Controller = New T_PoScreenControllerAddModeWritable
    LineController.ExecFormDelete New T_StatusConcreteGroupLinesReadOnly, PwCount
    
    'Assert:
    Assert.AreEqual CLng(1), PwCount.Counter

TestExit:
    '@Ignore UnhandledOnErrorResumeNext
    On Error Resume Next
    
    Exit Sub
TestFail:
    Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
    Resume TestExit
End Sub

The first line is a comment as far as VBA is concerned, but it is vital for the RubberDuckVBA test engine.

'@TestMethod("DeleteAction")

It is telling RubberDuck that the next method should be added to the Test Explorer and that it should be in the category entitled “DeleteAction”. This lets me sort my Test methods in various ways in the Test Explorer RubberDuckVBA provides.

Then there’s the actual name of the subroutine:

Private Sub GivenConcreteGroupDeleteTriggered_WhenReadOnlyStatusType_RequirePassword()

Yes, it is very long. I try to name my test subroutines in a Given, When, Then format.

Each section is separated by an underscore and I am capitalizing each word. So to put this into the intended meaning it says:

Given the concrete group delete is triggered, when read only is a status type, then require a password.

This may not make sense to you, but perhaps reading yesterday’s message explains more generally what I am trying to do and would help you understand this abbreviated version for my naming convention.

So my tests generally start with the word Given, then When, and I usually omit the Then, but that’s always the 3rd item after the underscore.

Next is the error clause, with the error labels it will go to at the end of the sub:

    On Error GoTo TestFail

...

TestExit:
    '@Ignore UnhandledOnErrorResumeNext
    On Error Resume Next
    
    Exit Sub
TestFail:
    Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
    Resume TestExit

This error handling was added by the default settings of RubberDuck VBA. During the test itself, if there are any errors at all, this handler will cause the test to fail with the Assert.Fail Method then go to the cleanup section of TestExit: and just end the routine. I suspect the ‘@Ignore UnhandledOnErrorResumeNext line might be a special instruction for RubberDuckVBA.

In any case, an error causes the test to fail and the Test Explorer will report the failure message of the test.

Tomorrow I will continue describing my test code.