Skip to content

Commit 522775a

Browse files
committed
nunit
1 parent cae7ff3 commit 522775a

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

BankNUnitTests/BankAccountTests.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Bank;
2+
using System;
3+
using NUnit.Framework;
4+
5+
namespace BankNUnitTests
6+
{
7+
public class BankAccountTests
8+
{
9+
private BankAccount account;
10+
11+
[SetUp]
12+
public void Setup()
13+
{
14+
// ARRANGE
15+
account = new BankAccount(1000);
16+
}
17+
18+
[Test]
19+
public void Adding_Funds_Updates_Balance()
20+
{
21+
// ACT
22+
account.Add(500);
23+
24+
// ASSERT
25+
Assert.AreEqual(1500, account.Balance);
26+
}
27+
28+
[Test]
29+
public void Adding_Negative_Funds_Throws()
30+
{
31+
// ACT + ASSERT
32+
Assert.Throws<ArgumentOutOfRangeException>(() => account.Add(-500));
33+
}
34+
35+
[Test]
36+
public void Withdrawing_Funds_Updates_Balance()
37+
{
38+
// ACT
39+
account.Withdraw(500);
40+
41+
// ASSERT
42+
Assert.AreEqual(500, account.Balance);
43+
}
44+
45+
[Test]
46+
public void Withdrawing_Negative_Funds_Throws()
47+
{
48+
// ACT + ASSERT
49+
Assert.Throws<ArgumentOutOfRangeException>(() => account.Withdraw(-500));
50+
}
51+
52+
[Test]
53+
public void Withdrawing_More_Than_Balance_Throws()
54+
{
55+
// ACT + ASSERT
56+
Assert.Throws<ArgumentOutOfRangeException>(() => account.Withdraw(2000));
57+
}
58+
59+
[Test]
60+
public void Transfering_Funds_Updates_Both_Accounts()
61+
{
62+
// ARRANGE
63+
var otherAccount = new BankAccount();
64+
65+
// ACT
66+
account.TransferFundsTo(otherAccount, 500);
67+
68+
// ASSERT
69+
Assert.AreEqual(500, account.Balance);
70+
Assert.AreEqual(500, otherAccount.Balance);
71+
}
72+
73+
[Test]
74+
public void Transfer_To_Non_Existing_Account_Throws()
75+
{
76+
// ACT + ASSERT
77+
Assert.Throws<ArgumentNullException>(() => account.TransferFundsTo(null, 2000));
78+
}
79+
}
80+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="NUnit" Version="3.12.0" />
11+
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Bank\Bank.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

UnitTestingTutorial.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30804.86
55
MinimumVisualStudioVersion = 15.0.26124.0
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bank", "Bank\Bank.csproj", "{FA0254C6-CC54-4A9C-9159-6BF8CB54F2FA}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BankNUnitTests", "BankNUnitTests\BankNUnitTests.csproj", "{B26B161C-31B7-43EC-9E56-981F64F82935}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,18 @@ Global
2729
{FA0254C6-CC54-4A9C-9159-6BF8CB54F2FA}.Release|x64.Build.0 = Release|Any CPU
2830
{FA0254C6-CC54-4A9C-9159-6BF8CB54F2FA}.Release|x86.ActiveCfg = Release|Any CPU
2931
{FA0254C6-CC54-4A9C-9159-6BF8CB54F2FA}.Release|x86.Build.0 = Release|Any CPU
32+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Debug|x64.ActiveCfg = Debug|Any CPU
35+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Debug|x64.Build.0 = Debug|Any CPU
36+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Debug|x86.ActiveCfg = Debug|Any CPU
37+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Debug|x86.Build.0 = Debug|Any CPU
38+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Release|x64.ActiveCfg = Release|Any CPU
41+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Release|x64.Build.0 = Release|Any CPU
42+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Release|x86.ActiveCfg = Release|Any CPU
43+
{B26B161C-31B7-43EC-9E56-981F64F82935}.Release|x86.Build.0 = Release|Any CPU
3044
EndGlobalSection
3145
GlobalSection(SolutionProperties) = preSolution
3246
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)