Chapter 14. Separate View from Model

“To each his own. (Suum Cuique)”

Cicero

Tests can be made more maintainable by separating what appears to the user from the logic in the underlying business model.


Decouple the User Interface

For the CD Reservation story in the previous chapter, the business rule (the model) for determining whether a customer is allowed to reserve CDs was documented, and tests were written for it. This reservation tests go to some module inside the system, as shown in Figure 14.1. The triad did not talk about how the reservation-allowed condition is displayed (the view), and they did not make up tests for the user interface.

Figure 14.1. User Interface and Logic Tests

image

There are at least three ways that the application could show how a customer was not allowed to reserve a CD:

• The application could hide the Reserve option.

• It could disable the Reserve option.

• It could have the Reserve option go to a dialog box that informs the customer that he is not allowed to reserve.

Here are the three ways displayed in tables.

image

Or

image

Or

image

Each variation can be described with a specific test, as shown here.


Decoupling Simplifies Testing

When Sam and Cathy change their minds about how the ability to reserve should be displayed, all Debbie and Tom have to do is change the test to one of these. These tests do not have to be automatic. Tom can manually run this test. If there gets to be too many of these manual tests, Tom could use a user interface test automation tool. The tool could be used when the application is being built to automatically verify the user interface, as shown in Chapter 3, “Testing Strategy.”

If the business logic had not been separated from the display logic, the test for the button Enabled/Disabled option might be as follows.

image

Whenever a change is made in how to display the allowable reservation, this bigger table has to be changed. Mistakes might be made at some point. This separation of state from display translates both into easier to test and simpler code. From a testing standpoint, Tom just has to confirm that the user interface displays the correct result in two cases—for 007 and 86. With this table, he has to see that the user interface displays the correct result in five cases—one for each row in the table.


Summary

• Decouple the user interface from the business logic (separate view from model) to simplify testing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.136