Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add basic test for clean architecture #5

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Dotnet.Template.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
60 changes: 60 additions & 0 deletions tests/Core.Tests/ArchitectureTests.cs
Original file line number Diff line number Diff line change
@@ -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));

Check warning on line 10 in tests/Core.Tests/ArchitectureTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
private static readonly Assembly InfrastructureAssembly = Assembly.GetAssembly(typeof(Infrastructure.DependencyInjection));

Check warning on line 11 in tests/Core.Tests/ArchitectureTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
private static readonly Assembly ApplicationAssembly = Assembly.GetAssembly(typeof(Application.DependencyInjection));

Check warning on line 12 in tests/Core.Tests/ArchitectureTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.
private static readonly Assembly DomainAssembly = Assembly.GetAssembly(typeof(Domain.Forecast));

Check warning on line 13 in tests/Core.Tests/ArchitectureTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.

[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();
}
}
29 changes: 29 additions & 0 deletions tests/Core.Tests/Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NetArchTest.Rules" Version="1.3.2" />
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Api\Api.csproj" />
</ItemGroup>

</Project>
Loading