Advertisement
BbJLeB

03. Bank Account

Dec 17th, 2023
968
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using NUnit.Framework;
  2. using System;
  3.  
  4. namespace TestApp.Tests
  5. {
  6.     [TestFixture]
  7.     public class BankAccountTests
  8.     {
  9.         [Test]
  10.         public void Test_Constructor_InitialBalanceIsSet()
  11.         {
  12.             // Arrange
  13.             double initialBalance = 100.0;
  14.  
  15.             // Act
  16.             BankAccount account = new BankAccount(initialBalance);
  17.  
  18.             // Assert
  19.             Assert.AreEqual(initialBalance, account.Balance);
  20.         }
  21.  
  22.         [Test]
  23.         public void Test_Deposit_PositiveAmount_IncreasesBalance()
  24.         {
  25.             // Arrange
  26.             BankAccount account = new BankAccount(100.0);
  27.             double depositAmount = 50.0;
  28.  
  29.             // Act
  30.             account.Deposit(depositAmount);
  31.  
  32.             // Assert
  33.             Assert.AreEqual(150.0, account.Balance);
  34.         }
  35.  
  36.         [Test]
  37.         public void Test_Deposit_NegativeAmount_ThrowsArgumentException()
  38.         {
  39.             // Arrange
  40.             BankAccount account = new BankAccount(100.0);
  41.             double depositAmount = -50.0;
  42.  
  43.             // Act & Assert
  44.             Assert.Throws<ArgumentException>(() => account.Deposit(depositAmount), "Deposit with negative amount should throw an ArgumentException.");
  45.         }
  46.  
  47.         [Test]
  48.         public void Test_Withdraw_ValidAmount_DecreasesBalance()
  49.         {
  50.             // Arrange
  51.             BankAccount account = new BankAccount(100.0);
  52.             double withdrawAmount = 50.0;
  53.  
  54.             // Act
  55.             account.Withdraw(withdrawAmount);
  56.  
  57.             // Assert
  58.             Assert.AreEqual(50.0, account.Balance);
  59.         }
  60.  
  61.         [Test]
  62.         public void Test_Withdraw_NegativeAmount_ThrowsArgumentException()
  63.         {
  64.             // Arrange
  65.             BankAccount account = new BankAccount(100.0);
  66.             double withdrawAmount = -50.0;
  67.  
  68.             // Act & Assert
  69.             Assert.Throws<ArgumentException>(() => account.Withdraw(withdrawAmount), "Withdraw with negative amount should throw an ArgumentException.");
  70.         }
  71.  
  72.         [Test]
  73.         public void Test_Withdraw_AmountGreaterThanBalance_ThrowsArgumentException()
  74.         {
  75.             // Arrange
  76.             BankAccount account = new BankAccount(100.0);
  77.             double withdrawAmount = 150.0;
  78.  
  79.             // Act & Assert
  80.             Assert.Throws<ArgumentException>(() => account.Withdraw(withdrawAmount), "Withdraw with amount greater than balance should throw an ArgumentException.");
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement