This documentation is archived and is not being maintained.

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:

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

Show: