단위 테스트 샘플

업데이트: 2007년 11월

"Woodgrove Bank" 샘플은 간단한 프로그램으로 빌드할 수 있는 코드로 구성되어 있습니다. 프로그램으로 빌드한 다음에는 Woodgrove Bank 프로그램의 다양한 메서드(공용 및 전용)를 테스트하는 단위 테스트를 생성할 수 있습니다.

이 샘플 코드는 다음 연습에서 사용하도록 제공됩니다.

샘플 코드

이 샘플의 최신 코드를 보려면 다음을 참조하십시오.

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 < 0)
            {
                throw new ArgumentOutOfRangeException("amount");
            }

            m_balance += amount;
        }

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

            if (amount > m_balance)
            {
                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. */
참고:

이 샘플의 이전 버전은 컴퓨터 하드 드라이브에 있는 Visual Studio Team System Test Edition의 설치 디렉터리에서 확인할 수 있습니다. 기본적으로 샘플은 설치 도중 \Program Files\Visual Studio 9\Samples\ 아래의 폴더에 복사됩니다. 이 예제의 경우 이 도움말 항목에서 가져오는 코드를 사용하는 것이 좋습니다.

코드 작업

이 코드로 작업하려면 먼저 Visual Studio에서 이를 위한 프로젝트를 만들어야 합니다. 연습: 단위 테스트 생성 및 실행에서 "연습 준비" 단원의 단계별 지침을 따릅니다.

참고 항목

작업

연습: 단위 테스트 생성 및 실행

연습: 테스트 실행 및 코드 검사 보기

연습: 명령줄 테스트 유틸리티 사용