BenMorris / NetArchTest

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

Sealed class assert is not working as expected #125

Closed fingers10 closed 1 year ago

fingers10 commented 1 year ago

I'm trying to Assert if all of my Request Class are sealed using the below test,

[Fact]
public void Should_Have_Request_To_Be_Sealed()
{
    var result =
        Types.InAssembly(typeof(ApplicationServiceRegistration).Assembly)
            .That()
            .AreClasses()
            .And()
            .Inherit(typeof(IRequest))
            .Should()
            .BeSealed()
            .GetResult();

    Assert.True(result.IsSuccessful);
}

Here are the examples of my Request Class,

public class CreateCategoryCommand : IRequest<Result<CategoryDto, BaseError>>
{
    public string Name { get; set; } = string.Empty;
    public string? Code { get; set; }
}

public class CreateClientCommand : IRequest<Result<ClientDto, BaseError>>
{
    public string Name { get; set; } = string.Empty;
}

As we can see, the Request Class are not sealed, but still the test gets passed.

Am I missing anything? Please assist.

NeVeSpl commented 1 year ago

Your interface is generic IRequest type differs from IRequest<>.

fingers10 commented 1 year ago

@NeVeSpl it gives wrong result i.e the test still passes even if I change IRequest to IRequest<>

NeVeSpl commented 1 year ago

The result is correct, your predicate is wrong. The second thing that is wrong is that interfaces are implemented not inherited.

[Fact]
public void Should_Have_Request_To_Be_Sealed()
{
    var result =
        Types.InAssembly(typeof(ApplicationServiceRegistration).Assembly)
            .That()
            .AreClasses()
            .And()
            .ImplementInterface(typeof(IRequest<>))
            .Should()
            .BeSealed()
            .GetResult();

    Assert.True(result.IsSuccessful);
}