Architecture test using NetArchTest gives false result when trying to assert sealed class fitness rule

huangapple go评论104阅读模式
英文:

Architecture test using NetArchTest gives false result when trying to assert sealed class fitness rule

问题

I'm writing Architecture Fitness Test using NetArchTest and 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.

英文:

I'm writing Architecture Fitness Test using NetArchTest and 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.

答案1

得分: 0

我明白了。我应该使用ImplementInterface而不是Inherit,如下所示,

[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);
}
英文:

I figured out. I should use ImplementInterface instead of Inherit as shown below,

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

    Assert.True(result.IsSuccessful);
}

huangapple
  • 本文由 发表于 2023年8月5日 02:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定