【问题标题】:NUnit testing to test protected variableNUnit 测试来测试受保护的变量
【发布时间】:2020-09-06 22:16:52
【问题描述】:

我有一类储蓄账户,它有存款和取款的方法。它允许用户在他们的帐户中设置初始金额。

using System;
using System.Collections.Generic;
namespace Bank
{
    public class Saving:Account{

        public int _number{get;}

        private static int Number=160140000;
        public Saving(string type, int initbal):base(type,initbal){ 

            Number++;
            _number=Number;
        }

        public override void Display(){

            Console.WriteLine("Account number: " + _number);
            Console.WriteLine("Account type: " + base._type);
            Console.WriteLine("Balance: RM" + base._initbal);


        }

   }
}

它继承自 account.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace Bank
{
    public abstract class Account{


        protected string _type;
        protected int _initbal;

        private List<Transaction> transactionsMade=new List<Transaction>();
        public Account(string type, int initbal){

            _type=type;
            _initbal=initbal;

        }

        public virtual int Deposit(int amount_depo, DateTime date, string note){

            if(amount_depo<0){
                Console.WriteLine("Amount must be more than 0");
            }

            else{
                _initbal=amount_depo+_initbal;

            }
            var deposit=new Transaction(amount_depo,date,note);
            transactionsMade.Add(deposit);

            return _initbal;


        }
        public virtual int Withdraw(int amount_with,DateTime date, string note){

            if(_initbal<amount_with){
                Console.WriteLine("Insufficient funds");
            }

            else if(amount_with<0){
                Console.WriteLine("Amount must be more than 0");

            }

            else{

                _initbal=_initbal-amount_with;
            }

            var withdraw=new Transaction(amount_with,date,note);
            transactionsMade.Add(withdraw);
            return _initbal;

        }

        public abstract void Display();

        public string TransHistory(){

            var log=new StringBuilder();
            log.AppendLine("Date\t\tTime\t\tAmount\t\tNote");
            foreach(var item in transactionsMade){

                log.AppendLine($"{item._date.ToShortDateString()}\t{item._date.ToShortTimeString()}\t\t{item._amount}\t\t{item._note}");


            }

            return log.ToString();
        }
    }

}

我想为提款做 Nunit 测试。如果用户没有足够的钱,初始余额将保持不变。这是我尝试过的,但我无法访问受保护的变量

        [TestCase()]
        public void TestSaving(){

            //adding 100 to initial balance
            Saving save1=new Saving("Saving",100);

            //Test withdraw money that is higher than initial balance
            int withdraw_amount=200;
            save1.Withdraw(withdraw_amount,DateTime.Now,"Test withdraw");
            Assert.AreEqual(100, save1._initbal);

        }

关于如何进行提款或存款的测试还有其他建议吗?

【问题讨论】:

  • 创建一个返回帐户当前余额的方法,例如 - GetBalance()?? (方法将返回 _initbal 的值) - 然后在测试中调用它
  • 哦,是的,应该考虑到这一点。谢谢

标签: c# oop


【解决方案1】:

即使提款不成功,您的 Withdraw 方法已经返回余额,因此将 TestSaving() 的最后两行更改为:

var balanceAfterWithdrawal = save1.Withdraw(withdraw_amount,DateTime.Now,"Test withdraw");
Assert.AreEqual(100, balanceAfterWithdrawal);

会起作用的。

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 2019-04-04
    • 2012-05-22
    • 2020-05-09
    • 1970-01-01
    相关资源
    最近更新 更多