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:

Sample Code

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

using System;

namespace BankAccountNS
{
    /// <summary> 
    /// Bank Account demo class. 
    /// </summary> 
    public class BankAccount
    {

        private bool frozenValue = false;

        private BankAccount()
        {
        }

        public BankAccount(string customerName, double balance)
        {
            CustomerName = customerName;
            Balance = balance;
        }

        public string CustomerName { get; private set; }
        

        public double Balance { get; private set;}

        public void Debit(double amount)
        {
            if (frozenValue)
            {
                throw new Exception("Account frozen");
            }

            if (amount < 0)
            {
                throw new ArgumentOutOfRangeException("amount");
            }

            Balance += amount;
        }

        public void Credit(double amount)
        {
            if (frozenValue)
            {
                throw new Exception("Account frozen");
            }

            if (amount > Balance)
            {
                throw new ArgumentOutOfRangeException("amount");
            }

            Balance += amount;
        }

        private void FreezeAccount()
        {
            frozenValue = true;
        }

        private void UnfreezeAccount()
        {
            frozenValue = false;
        }

        public static void Main()
        {
            BankAccount ba = new BankAccount("Mr. Bryan Walton", 11.99);

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

    }
}

/* 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. */
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. 

Note

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.

Working with the Code

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

See Also

Tasks

Walkthrough: Creating and Running Unit Tests

Walkthrough: Run Tests and View Code Coverage

Walkthrough: Using the Command-line Test Utility