VBA-tools / vba-test

Add testing and TDD to VBA on Windows and Mac
MIT License
202 stars 46 forks source link

Asserting Err Object #24

Closed robodude666 closed 6 years ago

robodude666 commented 6 years ago

It would be useful to be able to assert the Err object and ensure that either no error occurred as expected, or that the expected error was called:

With Suite.Test("calling AcceptsLongsOnly with a long should not raise an error")
    On Error Resume Next
    Err.Clear
    MyModule.AcceptsLongsOnly 123
    .ToNotBeError Err
    On Error GoTo 0
End With

With Suite.Test("calling AcceptsLongsOnly with non-longs should raise an error")
    On Error Resume Next
    Err.Clear
    MyModule.AcceptsLongsOnly "ABC"
    .ToBeError Err, vbObjectError + 10001, "Expected value to be Long."
    On Error GoTo 0

    On Error Resume Next
    Err.Clear
    MyModule.AcceptsLongsOnly True
    .ToBeError Err, vbObjectError + 10001, "Expected value to be Long."
    On Error GoTo 0
End With

Of course, the naming of ToNotBeError and ToBeError are only examples. It may be possible to build them into IsOk and NotOk, but I'd prefer a dedicated method with Err or Error in the name for clarity.

timhall commented 6 years ago

Thanks for the suggestion, I've added IsError and NotError. I tried passing in Err, but it converts it to just the error number when you pass it around, so instead they just check the current global Err.

.IsError
.IsError vbObjectError + 1
.IsError Source:="..."
.IsError Description:="..."
.IsError vbObjectError + 1, "Source", "Description"

.NotError

Closed by https://github.com/VBA-tools/vba-test/commit/d58af6ed603b417c8f708db99dbdc19112a890e6