Refactoring For Testability

· Nov 12, 08:07 PM

I often find myself wishing for nice little examples now, but thinking of them only later. In a blinding flash of the obvious, I realized that my web site is backed by a content management system, so I can just write them down now and point to them later.

Consider the following simple method.

Sub ShowLastUpdateMessage(d as Date) if d <> nil then MsgBox "Last update: " + d.SQLDateTime else MsgBox "You have never been updated." end if End Sub

How does one test this? The answer is that one does not, because one cannot in any reasonable way. But I can test this.

Function LastUpdateMessage(d as Date) as String if d <> nil then return "Last update: " + d.SQLDateTime else return "You have never been updated." end if End Function

The not-so-testable part

Sub ShowLastUpdateMessage(d as Date) Msgbox LastUpdateMessage(d) End Sub

is reduced to a single line that calls a framework method. If there is a problem here, it's probably material for a feedback report.

---

Comment

Textile Help