-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from afhswe/master
Add separate c# .net 5 version setting up xUnit .net and fluent assertions for test project;
- Loading branch information
Showing
14 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ _ReSharper.*/ | |
*.suo | ||
*.user | ||
_NCrunch_*/ | ||
.vs/ | ||
|
||
# Python Ignores | ||
*.pyc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
17 changes: 17 additions & 0 deletions
17
...-5/TripServiceKata/TripServiceKata/Exception/DependentClassCallDuringUnitTestException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
c#-dotnet-5/TripServiceKata/TripServiceKata/Exception/UserNotLoggedInException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
c#-dotnet-5/TripServiceKata/TripServiceKata/Trip/TripDAO.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
c#-dotnet-5/TripServiceKata/TripServiceKata/Trip/TripService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
c#-dotnet-5/TripServiceKata/TripServiceKata/TripServiceKata.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
c#-dotnet-5/TripServiceKata/TripServiceKata/User/UserSession.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
c#-dotnet-5/TripServiceKata/TripServiceKataTest/TripServiceKata.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
c#-dotnet-5/TripServiceKata/TripServiceKataTest/TripServiceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |