Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
132 lines (103 sloc) 4.53 KB
title ms.date ms.prod ms.technology ms.topic helpviewer_keywords ms.assetid caps.latest.revision ms.author manager
Sample Project for Creating Unit Tests | Microsoft Docs
11/15/2016
visual-studio-dev14
vs-ide-test
conceptual
unit test sample [Visual Studio]
unit tests, samples
db80aaf2-0652-4d3f-a8c5-2a98fd8502a2
32
gewarren
jillfra

Sample Project for Creating Unit Tests

[!INCLUDEvs2017banner]

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

Sample Code

The only intentional error in this sample is that the in Debit method "m_balance += amount" should have a minus not a plus sign before the equals sign.

using System;   
  
namespace BankAccountNS  
{  
    /// <summary>   
    /// Bank Account demo class.   
    /// </summary>   
    public class BankAccount  
    {  
        private string m_customerName;  
  
        private double m_balance;  
  
        private bool m_frozen = false;  
  
        private BankAccount()  
        {  
        }  
  
        public BankAccount(string customerName, double balance)  
        {  
            m_customerName = customerName;  
            m_balance = balance;  
        }  
  
        public string CustomerName  
        {  
            get { return m_customerName; }  
        }  
  
        public double Balance  
        {  
            get { return m_balance; }  
        }  
  
        public void Debit(double amount)  
        {  
            if (m_frozen)  
            {  
                throw new Exception("Account frozen");  
            }  
  
            if (amount > m_balance)  
            {  
                throw new ArgumentOutOfRangeException("amount");  
            }  
  
            if (amount < 0)  
            {  
                throw new ArgumentOutOfRangeException("amount");  
            }  
  
            m_balance += amount; // intentionally incorrect code  
        }  
  
        public void Credit(double amount)  
        {  
            if (m_frozen)  
            {  
                throw new Exception("Account frozen");  
            }  
  
            if (amount < 0)  
            {  
                throw new ArgumentOutOfRangeException("amount");  
            }  
  
            m_balance += amount;  
        }  
  
        private void FreezeAccount()  
        {  
            m_frozen = true;  
        }  
  
        private void UnfreezeAccount()  
        {  
            m_frozen = 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. */

Working with the Code

To work with this code, you first have to create a project for it in [!INCLUDEvsprvs]. Follow the steps in the "Prepare the Walkthrough" section in Walkthrough: Creating and Running Unit Tests for Managed Code.

See Also

Walkthrough: Creating and Running Unit Tests for Managed Code
Walkthrough: Run tests and view code coverage
Walkthrough: using the command-line test utility

You can’t perform that action at this time.