Export (0) Print
Expand All
2 out of 11 rated this helpful - Rate this topic

Unit Test Sample

The "Woodgrove Bank" sample consists of code that you can build into a simple program. You can then generate unit tests that test the various methods, both public and private, of the Woodgrove Bank program.

This sample code is provided for use in the following walkthroughs:

The most up-to-date code for this sample is available here:

Namespace BankAccountNS
    ''' <summary> 
    ''' Bank Account demo class. 
    ''' </summary> 
    Public Class BankAccount

        Private frozenValue As Boolean = False

        Private Sub New()
        End Sub

        Public Sub New(ByVal customerName As String, ByVal balanceAmount As Double)
            customerNameValue = customerName
            balanceValue = balanceAmount
        End Sub

        Public Property CustomerName() As String
            Get
                Return customerNameValue
            End Get
            Private Set(ByVal value As String)
                customerNameValue = value
            End Set
        End Property
        Private customerNameValue As String


        Public Property Balance() As Double
            Get
                Return balanceValue
            End Get
            Private Set(ByVal value As Double)
                balanceValue = value
            End Set
        End Property
        Private balanceValue As Double

        Public Sub Debit(ByVal amount As Double)
            If frozenValue Then
                Throw New Exception("Account frozen")
            End If

            If amount < 0 Then
                Throw New ArgumentOutOfRangeException("amount")
            End If

            Balance += amount
        End Sub

        Public Sub Credit(ByVal amount As Double)
            If frozenValue Then
                Throw New Exception("Account frozen")
            End If

            If amount > Balance Then
                Throw New ArgumentOutOfRangeException("amount")
            End If

            balanceValue += amount
        End Sub

        Private Sub FreezeAccount()
            frozenValue = True
        End Sub

        Private Sub UnfreezeAccount()
            frozenValue = False
        End Sub

        Public Shared Sub Main()
            Dim ba As New BankAccount("Mr. Bryan Walton", 11.99)

            ba.Credit(5.77)
            ba.Debit(11.22)
            Console.WriteLine("Current balance is ${0}", ba.Balance)
        End Sub

    End Class
End Namespace


' The example companies, organizations, products, domain names, e-mail
' addresses, logos, people, places, and events depicted herein are
' fictitious.  No association with any real company, organization, product,
' domain name, email address, logo, person, places, or events is intended
'or should be inferred. 
NoteNote:

You might notice an older version of this sample on your computer's hard drive in the installation directory of Visual Studio Team System Test Edition. By default, samples are copied during installation into a folder under \Program Files\Visual Studio 9\Samples\. For this sample, we recommend that you instead use the code that you obtain from this Help topic.

To work with this code, you first have to create a project for it in Visual Studio. Follow the steps in the "Prepare the Walkthrough" section in Walkthrough: Creating and Running Unit Tests

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.