BenMorris / NetArchTest

A fluent API for .Net that can enforce architectural rules in unit tests.
MIT License
1.53k stars 85 forks source link

Failure message reports the broken types, but not how they are broken #151

Open thomaseyde opened 4 months ago

thomaseyde commented 4 months ago

I have written my first test where I want to assert that a namespace only depends on itself. I think that should be an easy test to write, the use case should be very common, but the library doesn't support that.

The TestResult only contains failed types, and not the invalid types they depend on.

The test still fails after I have fixed the obvious mistakes. There are no more invalid using statements in my source, which make me suspect I use some of the global dependencies found in the generated GlobalUsings.g.cs:

global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
global using global::Xunit;

I included all of them in my test, but it still fails:

        var types = Types.InCurrentDomain();

        var result = types.That().ResideInNamespace("EventSourcingExplorations.EventSourcing")
                          .Should().OnlyHaveDependenciesOn(
                              "EventSourcingExplorations.EventSourcing",
                              "EventSourcingExplorations.EventSourcing.Exceptions",
                              // GlobalUsings.g.cs below
                              "System",
                              "System.Collections.Generic",
                              "System.IO",
                              "System.Linq",
                              "System.Net.Http",
                              "System.Threading",
                              "System.Threading.Tasks",
                              "Xunit",
                              "EventSourcingExplorations.EventSourcing.Exceptions"
                          )
                          .GetResult();

        result.FailingTypeNames.Should().BeEmpty();

This project is a wonderful initiative, and something really needed. But I cannot use this if I have to dig into generated files and understand the internals.

Please, add a member to TestResult which expose each broken type with their invalid dependencies. While not perfect, it would be so much easier to identify coding mistakes or to fine tune the tests.

NeVeSpl commented 2 months ago

First of all, when you specify a parent namespace for example System, ale the children namespaces with the same prefix are already included: System.IO, System.Linq ... so there is no need for that.

And regarding main problem, you can check NetArchTest.eNhancedEdition, it already explains why given type do not pass the test.

Image