Context
I wrote a small testing library in VBA, so I can write unit tests as follows:
'@ExpectedError()
Public Sub TestCannotRegisterLoggerTwice()
Dim logger As ILogger
Set logger = MockLogger.Create("TestLogger", TraceLevel)
LogManager.Register logger
LogManager.Register logger
End Sub
The above test passes:
2014-10-04 16:42:55 TestCannotRegisterLoggerTwice: [PASS]
And if I remove the attribute ExpectedError, this will not work:
2014-10-04 16:43:49 TestCannotRegisterLoggerTwice: [INCONCLUSIVE] - Test raised an error: Automation error
So far so good. Almost.
Testing errors work fine!
Now, if I do the test, drop the division by zero, for example:
'@ExpectedError()
Public Sub TestCannotRegisterLoggerTwice()
Dim logger As ILogger
Set logger = MockLogger.Create("TestLogger", TraceLevel)
Dim boom As Integer
boom = 1 / 0
LogManager.Register logger
LogManager.Register logger
End Sub
The test is still unconvincing, but this time the error was correctly raised:
2014-10-04 16:45:12 TestCannotRegisterLoggerTwice: [INCONCLUSIVE] - Test raised an error: Division by zero
The part that I don’t understand is that in both cases the error occurs in the same VBAProject - the first in the class called LogManager(SUT), the last in the class called LogManagerTests.
Here is the part that causes the expected error that I am testing for:
Public Sub Register(ByVal logger As ILogger)
If Not this.Loggers.Exists(logger.Name) Then
this.Loggers.Add logger.Name, logger
Else
Err.Raise LogManagerError.DuplicateLoggerError, "LogManager.Register", "There is already a logger registered with name '" & logger.Name & "'."
End If
End Sub
What I want
'@ExpectedError(-2147220406) (Long LogManagerError.DuplicateLoggerError) TestEngine.ReflectTestMethods:
For Each prospect In classMethods
If CanAddTestMethod(prospect, result) Then
Set tMethod = New TestMethod
Set tMethod.OwnerInstance = classInstance
tMethod.MethodName = prospect.name
If prospect.HasAttribute(ExpectedErrorAttribute) Then
If prospect.AttributeParameters(ExpectedErrorAttribute).Count <> 0 Then
Err.Raise vbObjectError + 1055, "TestEngine.ReflectTestMethods", "ExpectedErrorAttribute for test method '" & prospect.name & "' should not have parameters."
End If
'force an "Automation Error" to be expected,
'because VBA "eats" the expected error thrown in the SUT:
tMethod.ExpectedError = 440 ' prospect.AttributeParameters(ExpectedErrorAttribute).First
End If
result.Add tMethod.MethodName, tMethod
End If
Next
? , SUT ?
-,
, , :
'@ExpectedError()
Public Sub TestCannotRegisterLoggerTwice()
On Error GoTo CleanFail
Dim logger As ILogger
Set logger = MockLogger.Create("TestLogger", TraceLevel)
LogManager.Register logger
LogManager.Register logger
Exit Sub
CleanFail:
Dim errNumber As Long
errNumber = Err.Number
Dim errSource As String
errSource = Err.Source
Dim errDescription As String
errDescription = Err.Description
Err.Raise errNumber, errSource, errDescription
End Sub
errDescription / , - Automation 440.
, ?
EDIT: , "" , :
'@ExpectedError(5)
Public Sub TestCannotRegisterLoggerTwice()
Dim logger As ILogger
Set logger = MockLogger.Create("TestLogger", TraceLevel)
LogManager.Register logger
LogManager.Register logger
End Sub
... , TestEngine, @ExpectedError, # 5/ " " SUT ; . , SUT , .
VBA?