Test case

Joined
Jan 13, 2023
Messages
10
Reaction score
1
Hi,
Can anyone help with this scenario:

Using only NUnit Assert.AreEqual method, write tests for the BankAccount class that cover the following cases:
.The Deposit & Withdraw methods will not accept negative numbers.
.BankAccount cannot overstep its overdraft limit.
.The Deposit & Withdraw methods will deposit or withdraw the correct amount, respectively.
.The Withdraw & Deposit methods return the correct results.

C#:
public class BankAccount
    {
        public double Balance { get; private set; }
        public double OverdraftLimit { get; private set; }
        public BankAccount(double overdraftLimit)
        {
            this.OverdraftLimit = overdraftLimit > 0 ? overdraftLimit : 0;
        }

        public bool Deposit(double amount)
        {
            if(amount >=0)
            {
                this.Balance += amount;
                return true;
            }
            return false;
        }

        public bool Withdraw(double amount)
        {
            if(this.Balance - amount >= -this.OverdraftLimit && amount >= 0)
            {
                this.Balance -= amount;
                return true;
            }
            return false;
        }
    }

Each of the test case methods should be the Tester class & have the Test attribute.


C#:
using System;
using NUnit.Framework;

[TestFixture]
public class Tester
{  
    private double epsilon = 1e-6;

    [Test]
    public void BankAccountCannotHaveNegativeOverdraftLimit()
    {
        BankAccount account = new BankAccount(-20);
       
        Assert.AreEqual(0, account.OverdraftLimit, epsilon);
    }
}

TIA
 
Joined
Mar 31, 2023
Messages
95
Reaction score
8
Here's an example implementation of NUnit test methods that cover the scenarios you mentioned:

C#: [TestFixture] public class Tester {
private double epsilon = 1e-6;

[Test]
public void DepositShouldNotAcceptNegativeNumbers()
{
BankAccount account = new BankAccount(100);
bool result = account.Deposit(-50);
Assert.IsFalse(result);
Assert.AreEqual(0, account.Balance, epsilon);
}

[Test]
public void WithdrawShouldNotAcceptNegativeNumbers()
{
BankAccount account = new BankAccount(100);
bool result = account.Withdraw(-50);
Assert.IsFalse(result);
Assert.AreEqual(0, account.Balance, epsilon);
}

[Test]
public void BankAccountCannotOverstepOverdraftLimit()
{
BankAccount account = new BankAccount(100);
account.Deposit(50);
bool result = account.Withdraw(200);
Assert.IsFalse(result);
Assert.AreEqual(50, account.Balance, epsilon);
}

[Test]
public void DepositShouldDepositCorrectAmount()
{
BankAccount account = new BankAccount(100);
bool result = account.Deposit(50);
Assert.IsTrue(result);
Assert.AreEqual(50, account.Balance, epsilon);
}

[Test]
public void WithdrawShouldWithdrawCorrectAmount()
{
BankAccount account = new BankAccount(100);
account.Deposit(50);
bool result = account.Withdraw(25);
Assert.IsTrue(result);
Assert.AreEqual(25, account.Balance, epsilon);
}

[Test]
public void DepositShouldReturnTrueIfSuccessful()
{
BankAccount account = new BankAccount(100);
bool result = account.Deposit(50);
Assert.IsTrue(result);
}

[Test]
public void WithdrawShouldReturnTrueIfSuccessful()
{
BankAccount account = new BankAccount(100);
account.Deposit(50);
bool result = account.Withdraw(25);
Assert.IsTrue(result);
}

}

In the above code, each test method has a descriptive name that indicates the scenario being tested. Inside each test method, we create an instance of the BankAccount class, perform the necessary operations, and then use Assert.AreEqual and Assert.IsTrue/Assert.IsFalse to verify that the results match our expectations. We also use the epsilon variable to set a tolerance for comparing double values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,747
Members
45,545
Latest member
rapter____0

Latest Threads

Top