尝试使用 NUnit 测试构造函数时出现有关构造函数中参数的错误

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

我有一个银行账户 class。我们正在学习如何使用 NUnit 测试您的构造函数、对象和方法。

这是我的银行账户 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;
        }

    }
}

这是我的 "TestClass",我应该测试 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);
        }
    }
}

现在我只是随机测试 1 value/parameter 并且 IDE 给我一个错误说 "there is no argument given that corresponds to the required formal parameter accNum of BankAccount.BankAccount(int,AccountType)"

为什么会出现这个错误,如何解决?我错过了什么吗?

当您的 BankAccount class 没有无参数构造函数时,您正在调用无参数构造函数。

在您的 BankAccount class 中创建无参数构造函数,或者将参数正确传递到 new BankAccount();