Skip to content

Commit

Permalink
Merge pull request #65 from afhswe/master
Browse files Browse the repository at this point in the history
Add separate c# .net 5 version setting up xUnit .net and fluent assertions for test project;
  • Loading branch information
sandromancuso authored Jun 19, 2024
2 parents 603608e + cc8b245 commit 0ab8fc2
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _ReSharper.*/
*.suo
*.user
_NCrunch_*/
.vs/

# Python Ignores
*.pyc
Expand Down
31 changes: 31 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripServiceKata.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31624.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TripServiceKata", "TripServiceKata\TripServiceKata.csproj", "{055638A1-235E-41B0-9F42-09945B72A7AE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TripServiceKata.Tests", "TripServiceKataTest\TripServiceKata.Tests.csproj", "{B66884EC-CFF5-4852-B517-0974B18DCEBB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{055638A1-235E-41B0-9F42-09945B72A7AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{055638A1-235E-41B0-9F42-09945B72A7AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{055638A1-235E-41B0-9F42-09945B72A7AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{055638A1-235E-41B0-9F42-09945B72A7AE}.Release|Any CPU.Build.0 = Release|Any CPU
{B66884EC-CFF5-4852-B517-0974B18DCEBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B66884EC-CFF5-4852-B517-0974B18DCEBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B66884EC-CFF5-4852-B517-0974B18DCEBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B66884EC-CFF5-4852-B517-0974B18DCEBB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {358C5FF3-53E2-4DA6-9E2B-F13D88543A9A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Runtime.Serialization;

namespace TripServiceKata.Exception
{
[Serializable]
public class DependentClassCallDuringUnitTestException : System.Exception
{
public DependentClassCallDuringUnitTestException() : base() { }

public DependentClassCallDuringUnitTestException(string message, System.Exception innerException) : base(message, innerException) { }

public DependentClassCallDuringUnitTestException(string message) : base(message) { }

private DependentClassCallDuringUnitTestException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace TripServiceKata.Exception
{
[Serializable]
public class UserNotLoggedInException : System.Exception
{

}
}
6 changes: 6 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripServiceKata/Trip/Trip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TripServiceKata.Trip
{
public class Trip
{
}
}
14 changes: 14 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripServiceKata/Trip/TripDAO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using TripServiceKata.Exception;

namespace TripServiceKata.Trip
{
public class TripDAO
{
public static List<Trip> FindTripsByUser(User.User user)
{
throw new DependentClassCallDuringUnitTestException(
"TripDAO should not be invoked on an unit test.");
}
}
}
36 changes: 36 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripServiceKata/Trip/TripService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using TripServiceKata.Exception;
using TripServiceKata.User;

namespace TripServiceKata.Trip
{
public class TripService
{
public List<Trip> GetTripsByUser(User.User user)
{
List<Trip> tripList = new List<Trip>();
User.User loggedUser = UserSession.GetInstance().GetLoggedUser();
bool isFriend = false;
if (loggedUser != null)
{
foreach(User.User friend in user.GetFriends())
{
if (friend.Equals(loggedUser))
{
isFriend = true;
break;
}
}
if (isFriend)
{
tripList = TripDAO.FindTripsByUser(user);
}
return tripList;
}
else
{
throw new UserNotLoggedInException();
}
}
}
}
11 changes: 11 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripServiceKata/TripServiceKata.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="Trip\" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripServiceKata/User/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;

namespace TripServiceKata.User
{
public class User
{
private List<Trip.Trip> trips = new List<Trip.Trip>();
private List<User> friends = new List<User>();

public List<User> GetFriends()
{
return friends;
}

public void AddFriend(User user)
{
friends.Add(user);
}

public void AddTrip(Trip.Trip trip)
{
trips.Add(trip);
}

public List<Trip.Trip> Trips()
{
return trips;
}
}
}
28 changes: 28 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripServiceKata/User/UserSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using TripServiceKata.Exception;

namespace TripServiceKata.User
{
public class UserSession
{
private static readonly UserSession userSession = new UserSession();

private UserSession() { }

public static UserSession GetInstance()
{
return userSession;
}

public bool IsUserLoggedIn(User user)
{
throw new DependentClassCallDuringUnitTestException(
"UserSession.IsUserLoggedIn() should not be called in an unit test");
}

public User GetLoggedUser()
{
throw new DependentClassCallDuringUnitTestException(
"UserSession.GetLoggedUser() should not be called in an unit test");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripServiceKataTest/TripServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Xunit;
using FluentAssertions;

namespace TripServiceKata.Tests
{
public class TripServiceTest
{
[Fact]
public void Foo()
{
true.Should().Be(false);
}
}
}
35 changes: 35 additions & 0 deletions c#-dotnet-5/TripServiceKata/TripService_Original.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using TripServiceKata.Exception;

namespace TripServiceKata.Trip
{
public class TripService
{
public List<Trip> GetTripsByUser(User.User user)
{
List<Trip> tripList = new List<Trip>();
User.User loggedUser = UserSession.GetInstance().getLoggedUser();
bool isFriend = false;
if (loggedUser != null)
{
foreach(User.User friend in user.GetFriends())
{
if (friend.Equals(loggedUser))
{
isFriend = true;
break;
}
}
if (isFriend)
{
tripList = TripDAO.FindTripsByUser(user);
}
return tripList;
}
else
{
throw new UserNotLoggedInException();
}
}
}
}
25 changes: 25 additions & 0 deletions c#-dotnet-5/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Testing legacy code: Hard-wired dependencies
============================================

Code related to my [Testing legacy code: Hard-wired dependencies][1] blog post. Try not reading the blog post before doing the exercise yourself.

What is it about?
-----------------

Provides an example of existing code that needs to be unit tested. But there is one rule:

> We can't change any existing code if not covered by tests. The only exception is if we need to change the code to add unit tests, but in this case, just automated refactorings (via IDE) are allowed.
Although this is a very small piece of code, it has a lot of the problems that we find in legacy code.

Details
-------

If you want to give it a go, the starting point is [TripServiceTest.cs][3] and [TripService.cs][4]. Try unit testing it following the rule above.

For future comparisions, when you are done, you can always check [TripService_Original.cs][2]

[1]: http://craftedsw.blogspot.com/2011/07/testing-legacy-hard-wired-dependencies.html "Testing legacy code: Hard-wired dependencies blog post"
[2]: https://github.com/alastairs/trip-service-kata/blob/csharp-version/c%23/TripServiceKata/TripService_Original.cs "TripService_Original.cs"
[3]: https://github.com/alastairs/trip-service-kata/blob/csharp-version/c%23/TripServiceKata/TripServiceKata.Tests/TripServiceTest.cs "TripServiceTest.cs"
[4]: https://github.com/alastairs/trip-service-kata/blob/csharp-version/c%23/TripServiceKata/TripServiceKata/Trip/TripService.cs "TripService.cs"

0 comments on commit 0ab8fc2

Please sign in to comment.