Fluent Assertions 检查是否所有端点都具有特定的 swagger 属性
Fluent Assertions check if all endpoints have a specific swagger attribute
我想检查我的 ASP.NET 核心 API 控制器的所有端点是否有一个看起来像这样的属性:
[SwaggerResponse(HttpStatusCode.OK,typeof(*different types*))]
我使用 xUnit 和 Fluent Assertions 编写了这个:
[Fact]
public void EndpointsSwaggerAttribute()
{
typeof(BaseController).Methods().Should().BeDecoratedWith<SwaggerResponseAttribute>(s =>(s.StatusCode == HttpStatusCode.OK.ToString()));
}
但这并不完全有效。它总是通过测试。 Base controller是一个helperclass继承ControllerBase,所有controller都继承Base Controller
BaseController
有方法吗?如果没有,您需要首先列出具体类型并在其上使用 Methods
扩展方法。
但是,我实际上会编写 HTTP API 测试(使用 ASP.NET 核心 HostBuilder
)来验证您的 Swagger 端点的可观察输出是否正确。
目前您只能直接在 BaseController 中查看方法,您必须获取所有子项 class:
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");
}
}
}
我想检查我的 ASP.NET 核心 API 控制器的所有端点是否有一个看起来像这样的属性:
[SwaggerResponse(HttpStatusCode.OK,typeof(*different types*))]
我使用 xUnit 和 Fluent Assertions 编写了这个:
[Fact]
public void EndpointsSwaggerAttribute()
{
typeof(BaseController).Methods().Should().BeDecoratedWith<SwaggerResponseAttribute>(s =>(s.StatusCode == HttpStatusCode.OK.ToString()));
}
但这并不完全有效。它总是通过测试。 Base controller是一个helperclass继承ControllerBase,所有controller都继承Base Controller
BaseController
有方法吗?如果没有,您需要首先列出具体类型并在其上使用 Methods
扩展方法。
但是,我实际上会编写 HTTP API 测试(使用 ASP.NET 核心 HostBuilder
)来验证您的 Swagger 端点的可观察输出是否正确。
目前您只能直接在 BaseController 中查看方法,您必须获取所有子项 class:
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");
}
}
}