Thursday 30 November 2023

All 3 UnitTest Examples in ASP.NET Core

 We will create Class library Project


Then will create MSTest, NUnit, XUnit Project




Now we will start step by step

I. In Class library Project>>Class1.cs file and below are code snippet
namespace BankAccountLibrary
{
    public class BackAccount
    {
        private readonly string CustomerName;
        private double balance;
        public BackAccount(string cname, double bal)
        {
            CustomerName = cname;
            balance = bal;
        }

        public string GetCustomerName
        {
            get
            {
                return CustomerName;
            }
        }

        public double GetBalance
        {
            get
            {
                return balance;
            }
        }

        public void Debit(double amt)
        {
            if (amt > balance)
                throw new ArgumentOutOfRangeException("Insufficient balance. Cannot withdraw");
            if (amt <= 0)
                throw new ArgumentOutOfRangeException("Amount trying to withdraw is invalid");
            //intentionally made a error
            balance -= amt;
        }
        public void Credit(double amt)
        {
            if (amt <= 0)
                throw new ArgumentOutOfRangeException("Amount trying to withdraw is invalid");

            balance += amt;
        }

    }
}

II. In MSUnit Project>>UnitTest1.cs file and below are code snippet
using BankAccountLibrary;

namespace BankAccountMSTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Debit_WithValidAmount_UpdateBalance()
        {
            //Arrange
            double BeginningBalance = 20.25;
            double debitAmount = 4.75;
            double expectedBalance = 15.50;
            BackAccount acc = new BackAccount("Martin", BeginningBalance);
            //Act
            acc.Debit(debitAmount);

            //Assertion
            double actual = acc.GetBalance;
            Assert.AreEqual(expectedBalance, actual, 0.001, "Account not debited as data is wrong");
        }
    }
}

III. In NUnit Project>>UnitTest1.cs file and below are code snippet
using BankAccountLibrary;

namespace BankAccountNUnitTesting
{
    public class Tests
    {
        private BackAccount acc;
        [SetUp]
        public void Setup()
        {
            acc = new BackAccount("Martin", 500);
        }

        [Test]
        public void Test_Debit_Method()
        {
            double amt = 200;
            double expected = 400;
            acc.Debit(amt);
            var actual = acc.GetBalance;
            //Assert.AreEqual(expected, actual);
            Assert.That(actual, Is.EqualTo(expected));
        }
    }
}

IV. In XUnit Project>>UnitTest1.cs file and below are code snippet
using BankAccountLibrary;

namespace BankAccountXUnitTesting
{
    public class UnitTest1
    {
        private readonly BackAccount acc;
        public UnitTest1()
        {
            acc = new BackAccount("Martin", 500);
        }
        [Fact]
        public void Test1()
        {
            //Arrange
            var expected = 300;
            var amt = 200;
            //act
            acc.Debit(amt);
            var actual = acc.GetBalance;
            //assertion
            Assert.NotEqual(expected, actual);
        }
    }
}


No comments:

Post a Comment