Friday, December 4, 2009

Section 9.1.  Expected Errors with Calculations









9.1. Expected Errors with Calculations


Sometimes, we want to test that the test data is rejected, as expected. For example, consider Figure 9.1, in which a negative amount is used in the first test row.


Figure 9.1. Negative Amount

CalculateDiscount

amount

discount()

-100.00

0.00

1200.00

60.00



When this Fit table is run, we get the report (partly) shown in Figure 9.2, in which the program rejects the negative amount. The report also provides programmer-specific information about the error in the yellow-colored cell (see Plate 5), which we can ignore.



Figure 9.2. Negative Amount Is Rejected

[View full size image]




Assuming that our business rule stipulates that it doesn't make sense to calculate the discount on a negative amount, we'd expect to get an error in that first test row. We can express that expectation by using the special value error in the calculated column instead, as shown in the Fit test in Figure 9.3.


Figure 9.3. Use of error in ColumnFixture

CalculateDiscount

amount

discount()

-100.00

error

1200.00

60.00



The error cell is colored green if an error occurred (an exception), as shown in Figure 9.4. Otherwise, it is colored red.



Figure 9.4. Negative Amount in error, as Expected







It makes sense to include the error case here because there is only one. (Fit will complain about any values that are not numbers.) However, many values in a table may not be valid, such as the dates entered by the user through the user interface. In that case, it makes sense to split the table into two: one for defining valid date values and one for defining the calculations on the valid dates. This topic is covered further in Chapter 18.









    No comments: