Skip to content

Commit 422c8b6

Browse files
committed
Add XUnit
1 parent 855664b commit 422c8b6

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

BankXUnitTests/BankAccountTests.cs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using Bank;
2+
using System;
3+
using Xunit;
4+
5+
namespace BankXUnitTests
6+
{
7+
public class BankAccountTests
8+
{
9+
[Fact]
10+
public void Adding_Funds_Updates_Balance()
11+
{
12+
// ARRANGE
13+
var account = new BankAccount(1000);
14+
15+
// ACT
16+
account.Add(100);
17+
18+
// ASSERT
19+
Assert.Equal(1100, account.Balance);
20+
}
21+
22+
[Fact]
23+
public void Adding_Negative_Funds_Throws()
24+
{
25+
// ARRANGE
26+
var account = new BankAccount(1000);
27+
28+
// ACT + ASSERT
29+
Assert.Throws<ArgumentOutOfRangeException>(() => account.Add(-100));
30+
}
31+
32+
[Fact]
33+
public void Withdrawing_Funds_Updates_Balance()
34+
{
35+
// ARRANGE
36+
var account = new BankAccount(1000);
37+
38+
// ACT
39+
account.Withdraw(100);
40+
41+
// ASSERT
42+
Assert.Equal(900, account.Balance);
43+
}
44+
45+
[Fact]
46+
public void Withdrawing_Negative_Funds_Throws()
47+
{
48+
// ARRANGE
49+
var account = new BankAccount(1000);
50+
51+
// ACT + ASSERT
52+
Assert.Throws<ArgumentOutOfRangeException>(() => account.Withdraw(-100));
53+
}
54+
55+
[Fact]
56+
public void Withdrawing_More_Than_Funds_Throws()
57+
{
58+
// ARRANGE
59+
var account = new BankAccount(1000);
60+
61+
// ACT + ASSERT
62+
Assert.Throws<ArgumentOutOfRangeException>(() => account.Withdraw(2000));
63+
}
64+
65+
[Fact]
66+
public void Transfering_Funds_Updates_Both_Accounts()
67+
{
68+
// ARRANGE
69+
var account = new BankAccount(1000);
70+
var otherAccount = new BankAccount();
71+
72+
// ACT
73+
account.TransferFundsTo(otherAccount, 500);
74+
75+
// ASSERT
76+
Assert.Equal(500, account.Balance);
77+
Assert.Equal(500, otherAccount.Balance);
78+
}
79+
80+
[Fact]
81+
public void TransferFundsTo_Non_Existing_Account_Throws()
82+
{
83+
// ARRANGE
84+
var account = new BankAccount(1000);
85+
86+
// ACT + ASSERT
87+
Assert.Throws<ArgumentNullException>(() => account.TransferFundsTo(null, 2000));
88+
}
89+
}
90+
}

BankXUnitTests/BankXUnitTests.csproj

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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="Microsoft.NET.Test.Sdk" Version="16.7.1" />
11+
<PackageReference Include="xunit" Version="2.4.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
<PrivateAssets>all</PrivateAssets>
15+
</PackageReference>
16+
<PackageReference Include="coverlet.collector" Version="1.3.0">
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
<PrivateAssets>all</PrivateAssets>
19+
</PackageReference>
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<ProjectReference Include="..\Bank\Bank.csproj" />
24+
</ItemGroup>
25+
26+
</Project>

UnitTestingTutorial.sln

+14
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}") = "BankXUnitTests", "BankXUnitTests\BankXUnitTests.csproj", "{D35819B5-A383-49C2-9256-BAFCE475E07F}"
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+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Debug|x64.ActiveCfg = Debug|Any CPU
35+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Debug|x64.Build.0 = Debug|Any CPU
36+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Debug|x86.ActiveCfg = Debug|Any CPU
37+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Debug|x86.Build.0 = Debug|Any CPU
38+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Release|x64.ActiveCfg = Release|Any CPU
41+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Release|x64.Build.0 = Release|Any CPU
42+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Release|x86.ActiveCfg = Release|Any CPU
43+
{D35819B5-A383-49C2-9256-BAFCE475E07F}.Release|x86.Build.0 = Release|Any CPU
3044
EndGlobalSection
3145
GlobalSection(SolutionProperties) = preSolution
3246
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)