From 5655d31d65b2bc9af73f645fbed306a49dfd21a2 Mon Sep 17 00:00:00 2001 From: Christian Brevik Date: Sun, 13 Oct 2024 20:04:49 +0200 Subject: [PATCH] chore: Add basic test for clean architecture --- Dotnet.Template.sln | 7 ++++ tests/Core.Tests/ArchitectureTests.cs | 60 +++++++++++++++++++++++++++ tests/Core.Tests/Core.Tests.csproj | 29 +++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 tests/Core.Tests/ArchitectureTests.cs create mode 100644 tests/Core.Tests/Core.Tests.csproj diff --git a/Dotnet.Template.sln b/Dotnet.Template.sln index e8dcd07..3d8591f 100644 --- a/Dotnet.Template.sln +++ b/Dotnet.Template.sln @@ -23,6 +23,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{A26AADFF-6 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{1FB1E6F9-7773-45D5-B81A-17FB35551D3C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Tests", "tests\Core.Tests\Core.Tests.csproj", "{41513BF9-E555-48D2-8EE5-517CA1F225E8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -52,6 +54,10 @@ Global {67BE9569-2A93-4FB3-8181-9D99F5BF85F4}.Debug|Any CPU.Build.0 = Debug|Any CPU {67BE9569-2A93-4FB3-8181-9D99F5BF85F4}.Release|Any CPU.ActiveCfg = Release|Any CPU {67BE9569-2A93-4FB3-8181-9D99F5BF85F4}.Release|Any CPU.Build.0 = Release|Any CPU + {41513BF9-E555-48D2-8EE5-517CA1F225E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {41513BF9-E555-48D2-8EE5-517CA1F225E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {41513BF9-E555-48D2-8EE5-517CA1F225E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {41513BF9-E555-48D2-8EE5-517CA1F225E8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {C0414A8D-D741-43D2-AD38-46AEBB9698F6} = {0380555F-9F01-432A-BA57-FB1A4AB495B5} @@ -62,5 +68,6 @@ Global {8A813774-F511-4C23-991B-51AFB9026146} = {A26AADFF-6902-47E2-A76D-CD69AEBE1420} {1FB1E6F9-7773-45D5-B81A-17FB35551D3C} = {82DAE9A3-FF13-48FA-AE9C-A70249A26E1A} {67BE9569-2A93-4FB3-8181-9D99F5BF85F4} = {1FB1E6F9-7773-45D5-B81A-17FB35551D3C} + {41513BF9-E555-48D2-8EE5-517CA1F225E8} = {0380555F-9F01-432A-BA57-FB1A4AB495B5} EndGlobalSection EndGlobal diff --git a/tests/Core.Tests/ArchitectureTests.cs b/tests/Core.Tests/ArchitectureTests.cs new file mode 100644 index 0000000..a25a7c0 --- /dev/null +++ b/tests/Core.Tests/ArchitectureTests.cs @@ -0,0 +1,60 @@ +using System.Reflection; +using FluentAssertions; +using NetArchTest.Rules; +using Xunit.Abstractions; + +namespace Core.Tests; + +public class ArchitectureTests() +{ + private static readonly Assembly ApiAssembly = Assembly.GetAssembly(typeof(Api.Program)); + private static readonly Assembly InfrastructureAssembly = Assembly.GetAssembly(typeof(Infrastructure.DependencyInjection)); + private static readonly Assembly ApplicationAssembly = Assembly.GetAssembly(typeof(Application.DependencyInjection)); + private static readonly Assembly DomainAssembly = Assembly.GetAssembly(typeof(Domain.Forecast)); + + [Fact] + public void Api_Should_Not_Depend_On_Infrastructure_Except_For_Setting_Up_DependencyInjection() + { + var result = Types.InAssembly(ApiAssembly) + .That() + .DoNotHaveName("Program") + .ShouldNot() + .HaveDependencyOn("Infrastructure") + .GetResult(); + + result.IsSuccessful.Should().BeTrue(); + } + + [Fact] + public void Infrastructure_Should_Not_Depend_On_Api() + { + var result = Types.InAssembly(InfrastructureAssembly) + .ShouldNot() + .HaveDependencyOn("Api") + .GetResult(); + + result.IsSuccessful.Should().BeTrue(); + } + + [Fact] + public void Application_Should_Not_Depend_On_Api_Or_Infrastructure() + { + var result = Types.InAssembly(ApplicationAssembly) + .ShouldNot() + .HaveDependencyOnAny("Api", "Infrastructure") + .GetResult(); + + result.IsSuccessful.Should().BeTrue(); + } + + [Fact] + public void Domain_Should_Not_Have_Dependency_On_Any_Of_The_Other_Layers() + { + var result = Types.InAssembly(DomainAssembly) + .ShouldNot() + .HaveDependencyOnAny("Api", "Infrastructure", "Application") + .GetResult(); + + result.IsSuccessful.Should().BeTrue(); + } +} \ No newline at end of file diff --git a/tests/Core.Tests/Core.Tests.csproj b/tests/Core.Tests/Core.Tests.csproj new file mode 100644 index 0000000..0c885de --- /dev/null +++ b/tests/Core.Tests/Core.Tests.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + +