Muhammad Shaeel Abbas
Muhammad Shaeel Abbas

Reputation: 29

Getting Error about arguments in constructor while trying to test a constructor with NUnit

I have a bank account class. We're learning on how to test your constructors and objects and methods using NUnit.

This is my BankAccount class.

    using System;
namespace PT7BankSim
{
    public class BankAccount
    {
        private int _accNumber;
        private double _balance;
        private AccountType _type;

        public int AccNumber
        {
            get
            {
                return _accNumber;
            }
        }

        public double Balance
        {
            get
            {
                return _balance;
            }
        }



        public BankAccount(int accNum, AccountType type)
        {
            _balance = 0.00;
            _accNumber = accNum;
            _type = type;

        }

        public void Deposit(double amt)
        {
            _balance += amt;
        }

        public void Withdraw(double amt)
        {
            if (amt > _balance)
            {
                Console.WriteLine("\n\n Insufficient Balance in account: " + _accNumber);
            }
            else
            {
                _balance -= amt;
            }

        }

        public String Details()
        {
            String sDetails = _type + " Account" + "         : " + _accNumber + " Balance : " + _balance;
            return sDetails;
        }

    }
}

And this is my "TestClass", I am supposed to test the constructor of BankAccount

    using System;
using NUnit.Framework;
namespace PT7BankSim
{
    [TestFixture]
    public class TestBank
    {
        [Test]
        public void TestConstructor()
        {
            BankAccount TBA = new BankAccount();
            Assert.AreEqual(00, TBA.AccNumber);
        }
    }
}

Now I just randomly test out 1 value/parameter and the IDE gives me an error saying "there is no argument given that corresponds to the required formal parameter accNum of BankAccount.BankAccount(int,AccountType)"

Why is this giving this error and how to solve it? Am I missing something?

Upvotes: 1

Views: 76

Answers (1)

Sichi
Sichi

Reputation: 277

You are calling a parameterless constructor, when your BankAccount class doesn't have a parameterless constructor.

Either create a parameterless constructor in your BankAccount class, or correctly pass arguments into new BankAccount();

Upvotes: 3

Related Questions