Master Irfan Elahee
Master Irfan Elahee

Reputation: 185

NUnit testing to test protected variable

I have a class of savings account which has methods to deposit and withdraw money. It allows a user to set a initial amount in their account.

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);


        }

   }
}

It inherits from 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();
        }
    }

}

I want to do Nunit testing for the withdrawal. If the user does not have enough money, the initial balance will stay the same. This is what i have tried but i cant access the protected variable

        [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);

        }

Any other suggestions on how to do the testing for the withdrawal or deposit?

Upvotes: 0

Views: 490

Answers (1)

swatsonpicken
swatsonpicken

Reputation: 883

Your Withdraw method is already returning the balance even if the withdrawal was unsuccessful so changing the last two lines of TestSaving() to:

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

will work.

Upvotes: 0

Related Questions