How to Use VoltScript Testing Framework for Validation
Imagine we have the following class:
Class Person
Public firstName as String
Public lastName as String
Public age as Integer
Sub New()
End Sub
End Class
We may want to validate that a Person has been set up correctly, because properties are not set and validated during the constructor.
Setting up Person
We will create the Person and set property values, ready for validation. In reality, this might be a JSON object received or a VoltScript object pre-populated outside the script that wants to validate it.
Test Code
We need to pass a name to the TestSuite in line 1, even though it's not being used. The key is line 2, where we suppress the report. If we did not, when the code finishes and the TestSuite is deleted, the HTML reports would be generated - unnecessarily.
In lines 3 - 8 we run the tests. Age is an integer, so could potentially be a negative integer, or much bigger than the age a person could be.
Checking The Validation Ran
Checking validation worked is straightforward and done by calling the ranSuccessfully()
function in line 1. If it wasn't successful, you want to identify what went wrong. This is done in lines 5 - 9. We loop through the results, which gives us access to the TestCase object. If the outcome was not "Passed", we capture the reason - the TestCase's errorMsg. This would be something like "Expected: 1, but was: 3".