C# 流畅的断言检查是否所有端点都具有特定的招摇过市属性

C# 流畅的断言检查是否所有端点都具有特定的招摇过市属性,c#,asp.net-core,swagger,xunit,fluent-assertions,C#,Asp.net Core,Swagger,Xunit,Fluent Assertions,我想检查我的ASP.NET核心API控制器的所有端点是否都有一个如下所示的属性: [SwaggerResponse(HttpStatusCode.OK,typeof(*different types*))] 我使用xUnit和Fluent断言写了以下内容: [Fact] public void EndpointsSwaggerAttribute() { typeof(BaseController).Methods().Should().BeDecoratedWith<Swag

我想检查我的ASP.NET核心API控制器的所有端点是否都有一个如下所示的属性:

[SwaggerResponse(HttpStatusCode.OK,typeof(*different types*))]
我使用xUnit和Fluent断言写了以下内容:

[Fact]
public void EndpointsSwaggerAttribute()
{
      typeof(BaseController).Methods().Should().BeDecoratedWith<SwaggerResponseAttribute>(s =>(s.StatusCode == HttpStatusCode.OK.ToString()));
}
[事实]
public void endpointsWaggerAttribute()
{
typeof(BaseController.Methods().Should().BeDecoratedWith(s=>(s.StatusCode==HttpStatusCode.OK.ToString());
}

但这并不完全有效。它总是通过测试。基本控制器是继承ControllerBase的帮助器类,所有控制器都继承基本控制器。

基本控制器是否有方法?如果没有,您需要首先列出具体类型,并使用
方法
扩展方法


但是,我实际上会编写HTTP API测试(使用ASP.NET Core
HostBuilder
),以验证您的Swagger端点的可观察输出是否正确。

BaseController是否有任何方法?如果没有,您需要首先列出具体类型,并使用
方法
扩展方法


但是,我实际上会编写HTTP API测试(使用ASP.NET Core
HostBuilder
),以验证您的Swagger端点的可观察输出是否正确。

目前您仅直接在BaseController中查看方法,您必须获取所有子类:

        var baseControllerType = typeof(BaseController);
        var controllerTypes = baseControllerType.Assembly.GetTypes().Where(t => t.IsClass && t != type
                                                      && type.IsAssignableFrom(BaseController))

然后,对于每个控制器,您可以应用相同的逻辑。

目前,您只直接在BaseController中查看方法,必须获取所有子类:

        var baseControllerType = typeof(BaseController);
        var controllerTypes = baseControllerType.Assembly.GetTypes().Where(t => t.IsClass && t != type
                                                      && type.IsAssignableFrom(BaseController))

然后,对于每个控制器,您可以应用相同的逻辑。

如果要检查API控制器的所有端点是否都具有SwaggerResponse属性,您需要首先获取API项目的程序集,然后获取项目中的所有方法:

public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        //if the unit test exsit in the api project...
        //Assembly asm = Assembly.GetExecutingAssembly();

        //if your unit test project seprate from the api project
        //you could get the api project assembly like below
        var asm = typeof(WeatherForecastController).Assembly;
        
        //get all the methods in project
        var methods = asm.GetTypes()
        .Where(type => typeof(ControllerBase).IsAssignableFrom(type)) 
        .SelectMany(type => type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)).ToList();
                    
        foreach (var method in methods)
        {              
            //check if the method has SwaggerResponse attribute  
            var result = Attribute.IsDefined(method, typeof(SwaggerResponseAttribute));
            Assert.True(result, $"{method.Name} should be declared with SwaggerResponse Attribute");
        }

    }
}

如果要检查API控制器的所有端点是否具有SwaggerResponse属性,则需要首先获取API项目的程序集,然后获取项目中的所有方法:

public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        //if the unit test exsit in the api project...
        //Assembly asm = Assembly.GetExecutingAssembly();

        //if your unit test project seprate from the api project
        //you could get the api project assembly like below
        var asm = typeof(WeatherForecastController).Assembly;
        
        //get all the methods in project
        var methods = asm.GetTypes()
        .Where(type => typeof(ControllerBase).IsAssignableFrom(type)) 
        .SelectMany(type => type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)).ToList();
                    
        foreach (var method in methods)
        {              
            //check if the method has SwaggerResponse attribute  
            var result = Attribute.IsDefined(method, typeof(SwaggerResponseAttribute));
            Assert.True(result, $"{method.Name} should be declared with SwaggerResponse Attribute");
        }

    }
}


您只检查BaseController,而不是所有继承自BaseController的控制器您只检查BaseController,而不是所有继承自BaseController的控制器,以便建议通过实际HTTP API测试进行验证;)我完全同意在self-host中运行web api进行测试以获得炫耀,但如何确保没有遗漏注释?您需要该注释以使炫耀以某种方式返回某些内容,对吗?如果您知道这一点,我还希望有一个HTTP测试来验证这一点。但是注释纯粹是为了文档目的。因此,您可以使用HTTP调用来检查端点是否返回了预期的类型,也可以使用HTTP调用来检查是否正确生成了Swagger,但这并不意味着符号/返回类型文档已应用于端点,并且已存在于Swagger中。您可以潜在地解析Swagger,并真正尝试每个调用,以查看您得到的是否与Swagger声明的类型相对应,但检查注释就足够了,这似乎有点过分了。不,这是一个选择问题。我不喜欢在测试中查看内部实现细节。建议通过实际的HTTP API测试进行验证;)我完全同意在self-host中运行web api进行测试以获得炫耀,但如何确保没有遗漏注释?您需要该注释以使炫耀以某种方式返回某些内容,对吗?如果您知道这一点,我还希望有一个HTTP测试来验证这一点。但是注释纯粹是为了文档目的。因此,您可以使用HTTP调用来检查端点是否返回了预期的类型,也可以使用HTTP调用来检查是否正确生成了Swagger,但这并不意味着符号/返回类型文档已应用于端点,并且已存在于Swagger中。您可以潜在地解析Swagger,并真正尝试每个调用,以查看您得到的是否与Swagger声明的类型相对应,但检查注释就足够了,这似乎有点过分了。不,这是一个选择问题。我不喜欢在测试中查看内部实现细节。我不明白为什么要使用
type
。你的意思是使用baseControllerType吗?是的,对不起,我在本地制作了一个小样本,但忘了更改名称:我不明白为什么要使用
type
。你的意思是使用baseControllerType吗?是的,对不起,我在本地制作了一个小样本,但忘了更改名称:s这正是我想要的,谢谢。这个解决方案是否可以用来检查所有方法是否都有一个带有特定状态代码的SwiggerResponseAttribute?这正是我想要的,谢谢。是否可以使用此解决方案检查所有方法是否都具有带有特定状态代码的SwaggerResponseAttribute?