按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
class; like this:
Class TestCurrencyTrader
Inherits CurrencyTrader
Public Sub InitializeExchangeRate()
ExchangeRate = 100。0
If ExchangeRate 100。0 Then
Throw New Exception(〃100。0 verification failed〃)
End If
End Sub
End Class
The bolded code illustrates the verification code used to ensure that the value assigned to
ExchangeRate is the same one that is retrieved。
■Note The tests we’re using are being more plicated; and you may wonder; “Why do it that way?”
For this book; we are writing tests and creating our own testing framework。 Normally you would not do that。
You would use a testing framework such as NUnit ( http://nunit。org) or the Microsoft Visual Studio
Professional tools to help create the tests。 But here I want to demonstrate how to use Visual Basic; not a
testing tool。 By learning how to write the tests from the ground up; you will understand what to expect from
testing frameworks。
Using Conditional Statements
Having verification code within a class is acceptable in the context of the test class
TestCurrencyTrader。 However; the problem of testability is still present in those classes that do
not expose their state。
…………………………………………………………Page 171……………………………………………………………
CH A PT E R 6 ■ L E A R N I N G T HE B AS IC S O F O B J E CT OR I E N TE D P R O G R AM M IN G 149
To understand the problem; let’s return to the code for preheating the oven that you saw
earlier in the section about the problems with properties。 Imagine rewriting Oven to include a
verification test; like this:
Class Oven
Private _temperature As Integer
Public Sub SetTemperature(ByVal temperature As Integer)
_temperature = temperature
If _temperature 100。0 Then
Throw New Exception(〃100。0 verification failed〃)
End If
End Sub
Public Function AreYouPreHeated() As Boolean
' Check if oven temperature matches prescribed temperature
Return False
End Function
End Class
The bolded code illustrates a verification much like that used in CurrencyTrader; which
verifies the parameter temperature for a specific value。 While the verification is useful for a
particular test; it is not useful in the big picture。 As the code is written; the only valid value of
temperature is 100。0; anything else will generate an exception。 It is definitely not a solution。
To get around this problem; you could use Visual Basic conditional statements。 Condi
tional statements are special keywords that allow a developer to define whether a piece of
source code is piled。 Following is an example of source code that includes conditional
statements。
Class TestCurrencyTrader
Inherits CurrencyTrader
Public Sub InitializeExchangeRate()
ExchangeRate = 100。0
#If INTEGRATE_TESTS Then
if ExchangeRate 100。0 Then
Throw New Exception(〃100。0 verification failed〃)
End If
#End If
End Sub
End Class
The conditional statement always starts with a hash character (#); immediately followed
by a keyword—If in this example。 Between the #If and #End If is code that is conditionally
piled。 This is known as a preprocessor directive。 In this example; the condition means
to pile the code contained within the #If and #End If block if the value INTEGRATE_TESTS
is true。
You can define a pilation identifier like INTEGRATE_TESTS in the source code or in your
IDE。 In Visual Basic Express; you can assign INTEGRATE_TESTS as follows:
…………………………………………………………Page 172……………………………………………………………
150 CH AP T E R 6 ■ L E A R N IN G T HE B AS IC S O F OB J E CT OR I E N T E D P R O G R AM M IN G
1。 Right…click your project and select Properties。
2。 Select the pile tab。
3。 Click the Advanced pile Options button。
4。 Add the INTEGRATE_TESTS value to the Custom Constants text box。
Using Partial Classes for Verification
Conditional pilation is useful when you want to include or remove code depending on a
configuration。 However; some programmers will frown upon having conditional pilation
code within a function; as that would be a maintenance nightmare。 Another solution is to use
the Partial class keyword in conjunction with conditional pilation statements。
Thus far in all of the examples; whenever we defined a class; all of the methods and code
were declared between the Class/End Class pair。 By using partial classes; it is possible to declare
a class in multiple places。 When the Visual Basic piler piles the source code; the individual
class pieces will be assembled into a single class definition。 For our test code; we can create an
implementation partial class and a conditionally piled test partial class implementation。
The following is the modified TestCurrencyTrader class that could be used to test the state
without exposing the state。
Partial Class TestCurrencyTrader
Inherits CurrencyTrader
Public Sub InitializeExchangeRate()
ExchangeRate = 100。0
End Sub
End Class
#if INTEGRATE_TESTS Then
Partial Class TestCurrencyTrader
Inherits CurrencyTrader
Public Sub VerifyExchangeRate(ByVal value As Double)
If ExchangeRate value The