梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
another application in the next section。
Namespace Class Method
Dim total As Integer = Calculator。Operations。Add��1�察�2��
total is an integer declared 1 and 2 are integer values�察�
variable that stores the result which represent the two
of the addition numbers that are added together
Figure 2´7。 The Add�┌� method is called by referencing the namespace and class containing the
method。 A period is used to separate the identifiers。
The caller must do two things�此 �
o Reference the correct bination of namespace�察�class�察�and method identifiers。
o Pass the correct types into the method�察�as specified by the method signature。
In the example�察�the addition of 1 and 2 results in 3�察�and therefore the variable total should
contain the value 3 ��the equal sign assigns the value returned from the method to the variable
on its left��。 I say ^should contain the value�察院�because when writing code�察�you are not always
sure。 Sometimes the code you write will be wrong because you overlooked something or forgot
to reference something。
Look at the calling code�察�and ask yourself if you are guaranteed that calling Add�┌� with 1
and 2 will result in 3。 The answer is that�察�as a caller�察�you cannot be 100�ァ�sure that the total variable
will contain 3。 Just because a box has the label ^Dishes ̄ does not necessarily mean that dishes
´´´´´´´´´´´´´´´´´´´´´´Page 57´´´´´´´´´´´´´´´´´´´´´´´
CH A PT E R 2 * L E A R N I N G A B OU T 。 N E T N U M B E R AN D V A L U E T Y P E S 35
are in the box。 You think you know the contents�察�but you cannot be 100�ァ�sure until you open
the box。 Likewise�察�in code�察�you need to look at how the Add�┌� method is implemented to be sure
of the contents of the total variable。
In a production coding session�察�looking at the implementation code to verify it is doing
what you expect is not a feasible solution�察�because that would take too much time and be
pletely unreliable。 The only real solution is to write test code。
Writing Code to Test the Add�┌� Method
Test code is caller code that passes parameters with targeted values and expects a targeted
answer。 If the caller does not get the targeted answer�察�then the implementation of the tested
method is wrong。 Figure 2´8 shows sample caller code that tests the Add�┌� operation ��we¨ll add
this to a project next��。
Targeted caller code that adds
1 and 2 and assigns the result
to the variable total
Dim total As Integer = Operations。Add��1�察�2��
If ��total 3�� Then
Console。WriteLine�─�Oops 1 add 2 does not equal 3;��
End If
Targeted testing If targeted testing fails�察�the
of the variable text ^Oops´ ̄ is generated�察�
total�察�paring it indicating an error
to the value 3
Figure 2´8。 Testing the Add�┌� method
The calling code of the test bears an uncanny resemblance to the code you saw in the previous
section。 The difference is that the test code uses targeted variables and values�察�whereas the
other code could contain any variables and values。 Another requirement of test code is to verify
the answers returned by the method with targeted responses。 The If statement is used to check
whether the value of the variable total is equal to 3。
When writing test code�察�the way the Add�┌� method is used must be the same way the
console or Windows application uses the method。 Otherwise�察�it would be like testing a winter
tire in the middle of the Sahara!fun to do but irrelevant。
Another question related to testing has to do with the timing of tests。 Do you create the
tests before or after implementing the Add�┌� method�拭�To get a clear understanding of the problem�察 �
imagine the development of a tire。 Do you define the tests for the tire before or after the tire has
been developed�拭�Most likely�察�the answer is before�察�during�察�and after development。 This is an
important consideration when developing software。 Tests are written before�察�during�察�and after
implementation�察�as follows�此�
´´´´´´´´´´´´´´´´´´´´´´Page 58´´´´´´´´´´´´´´´´´´´´´´´
36 CH AP T E R 2 * L E A R N IN G AB OU T 。 N E T N U M B E R A N D V A L U E T Y P E S
o You develop tests before implementing the Add�┌� method to get an idea of what name
spaces�察�classes�察�and methods you will be defining。 The definition of the different items
gives the developer an idea of how the items will be used。
o You develop tests during the implementation of the Add�┌� method to verify that your
source code implementation is on the right track。
o You develop tests after the implementation of the Add�┌� method as an exhaustive measure
to make sure you¨ve dotted the i¨s and crossed the t¨s in the implementation。
Adding a Test Project to Your Solution
When writing test routines�察�you will need to organize the source code�察�and that means figuring
out to which project the tests are added。 For the calculator application�察�you could place the test
routines within the Calculator class library。 However�察�doing that is not the proper approach
due to distribution of the class library and correct testing context。 Remember that the test
routines must be identical to how the code will be used。 Thus�察�the appropriate place for the test
routines is in their own application。
The ideal approach is to create another application that represents the tests。 Figure 2´5
illustrated how a Windows and console application could use the Calculator class library。
Figure 2´9 adds the testing console application that also uses the class library。
Figure 2´9。 Adding the testing console application
The testing console application is like the console application created in Chapter 1�察�and it
references the Calculator class library。 Both projects should be part of the Calculator solution。
Go ahead and add the TestCalculator project to the Calculator solution。 Remember to
add a reference to the Calculator class library ��right´click TestCalculator and choose Add
Reference Projects Calculator��。 Remember to set TestCalculator as the startup project for
debugging purposes。 Figure 2´10 shows the TestCalculator and Calculator projects in the
Solution Explorer。
´´´´´´´´´´´´´´´´´´´´´´Page 59´´´´´´´´´´´´´´´´´´´´´´´
CH A PT E R 2 * L E A R N I N G A B OU T 。 N E T N U M B E R AN D V A L U E T Y P E S 37
Figure 2´10。 Solution Explorer showing the testing console application and Calculator
class library
Testing Simple Addition
Add the boldfaced code to Module1。vb in the testing console project to verify the addition of 1
and 2 ��the operator means is not equal to���此�
Imports Calculator
Module Module1
Public Sub TestSimpleA