From eba6852a0de7cedef50807c28f5cec641e0cbcef Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Tue, 18 Jun 2024 16:20:51 +0200 Subject: [PATCH] test: add ArchUnit test to assert the existance of empty default constructors --- Backbone.Tests.ArchUnit/DomainDrivenDesign.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Backbone.Tests.ArchUnit/DomainDrivenDesign.cs diff --git a/Backbone.Tests.ArchUnit/DomainDrivenDesign.cs b/Backbone.Tests.ArchUnit/DomainDrivenDesign.cs new file mode 100644 index 0000000000..6799f2fb5a --- /dev/null +++ b/Backbone.Tests.ArchUnit/DomainDrivenDesign.cs @@ -0,0 +1,28 @@ +using ArchUnitNET.Domain.Extensions; +using ArchUnitNET.Fluent.Conditions; +using ArchUnitNET.xUnit; +using Backbone.BuildingBlocks.Domain; +using static ArchUnitNET.Fluent.ArchRuleDefinition; + +namespace Backbone.Backbone.Tests.ArchUnit; + +public class DomainDrivenDesign +{ + [Fact] + public void EntitiesShouldHaveEmptyDefaultConstructors() + { + Types() + .That().AreAssignableTo(typeof(Entity)) + .Should().FollowCustomCondition((type) => + { + var constructors = type.GetConstructors(); + + if (constructors.All(c => c.Parameters.Count() != 0)) + return new ConditionResult(type, false, "Entity should have a parameterless constructor"); + + return new ConditionResult(type, true); + }, "") + .Because("otherwise the real constructor would be called by EF Core, which would add a domain event to the collection") + .Check(Backbone.ARCHITECTURE); + } +}