Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Net healthcheck中的模拟IEndpointRouteBuilder_C#_.net_Moq_Xunit - Fatal编程技术网

C# .Net healthcheck中的模拟IEndpointRouteBuilder

C# .Net healthcheck中的模拟IEndpointRouteBuilder,c#,.net,moq,xunit,C#,.net,Moq,Xunit,我创建了一个自定义的.Net健康检查 public static class HealthCheckHighMark { public static IEndpointConventionBuilder MapHighMarkChecks( this IEndpointRouteBuilder endpoints) { return endpoints.MapHealthChecks("/api/health/highmark&quo

我创建了一个自定义的.Net健康检查

public static class HealthCheckHighMark
{

    public static IEndpointConventionBuilder MapHighMarkChecks(
        this IEndpointRouteBuilder endpoints)
    {

        return endpoints.MapHealthChecks("/api/health/highmark", new HealthCheckOptions
        {
            ResponseWriter = async (context, report) =>
            {
                var result = JsonConvert.SerializeObject(
                    new HighMarkResult
                    {
                        HighMark = HealthHandler.GetHighMark().High.ToString(),
                    }, Formatting.None,
                    new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });
                context.Response.ContentType = MediaTypeNames.Application.Json;
                await context.Response.WriteAsync(result);
            },

            Predicate = (check) => check.Tags.Contains("highmark")
        });
    }
}
我正在尝试编写一个测试,以确保响应的格式正确。目前看起来是这样的:

readonly Mock<IEndpointRouteBuilder> _mockIEndpointRouteBuilder = new Mock<IEndpointRouteBuilder>();

[Fact]
public async Task TestHighMarkResponse()
{
    var _mockIHighMarkResult = Mock.Of<HighMarkResult>(m =>
        m.HighMark == "100");

    var response = HealthCheckHighMark.MapHighMarkChecks(_mockIEndpointRouteBuilder.Object);
}
readonly Mock\u mockIEndpointRouteBuilder=new Mock();
[事实]
公共异步任务TestHighMarkResponse()
{
var\u mockIHighMarkResult=Mock.Of(m=>
m、 高分==“100”);
var response=HealthCheckHighMark.maphhighmarkchecks(_mockIEndpointRouteBuilder.Object);
}
我的问题是,当我运行测试时,会出现以下错误:

信息: System.NullReferenceException:对象引用未设置为对象的实例。 堆栈跟踪: HealthCheckEndpointRouteBuilderExtensions.MapHealthChecksCore(IEndpointRouteBuilder端点、字符串模式、HealthCheckOptions) HealthCheckEndpointRouteBuilderExtensions.MapHealthChecks(IEndpointRouteBuilder端点、字符串模式、HealthCheckOptions) HealthCheckHighMark.MapHighMarkChecks(IEndpointRouteBuilder端点)第26行 HealthCheckTests.TestHighMarkResponse()行108 ---来自引发异常的上一个位置的堆栈结束跟踪---

问题似乎是
MapHigharkChecks
正在返回
null
。如何修复此问题,使其返回
HighMarkResult

这似乎是一个错误

我正在尝试编写一个测试,以确保响应的格式正确

但是测试正在尝试调用健康检查配置/设置

ResponseWriter
委托移动到其自己的字段中

public static class HealthCheckHighMark {
    public static IEndpointConventionBuilder MapHighMarkChecks(
        this IEndpointRouteBuilder endpoints) {
        return endpoints.MapHealthChecks("/api/health/highmark", new HealthCheckOptions {
            ResponseWriter = ResponseWriter, //<---
            Predicate = (check) => check.Tags.Contains("highmark")
        });
    }
    
    public static Func<HttpContext, HealthReport, Task> ResponseWriter = 
        async (context, report) => {
            var result = new HighMarkResult {
                HighMark = HealthHandler.GetHighark().High.ToString(),
            };
            var settings = new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            };
            string json = JsonConvert.SerializeObject(result, Formatting.None,settings);
            context.Response.ContentType = MediaTypeNames.Application.Json;
            await context.Response.WriteAsync(json);
        };
}
公共静态类HealthCheckHighMark{
公共静态IEndpointConventionBuilder映射高标记检查(
此IEndpointRouteBuilder(IEndpointRouteBuilder端点){
返回endpoints.MapHealthChecks(“/api/health/highmark”),新的HealthCheckOptions{
ResponseWriter=ResponseWriter,//check.Tags.Contains(“highmark”)
});
}
公共静态函数响应编写器=
异步(上下文、报告)=>{
var结果=新的HighMarkResult{
HighMark=HealthHandler.GetHighark().High.ToString(),
};
var设置=新的JsonSerializerSettings{
NullValueHandling=NullValueHandling.Ignore
};
字符串json=JsonConvert.SerializeObject(结果、格式、无、设置);
context.Response.ContentType=MediaTypeNames.Application.Json;
wait context.Response.WriteAsync(json);
};
}
这样就可以单独测试了

[Fact]
public async Task TestHighMarkResponse() {
    // Arrange
    HttpContext context = new DefaultHttpContext();
    var report = Mock.Of<HealthReport>();
    
    Stream body = new MemoryStream();
    context.Response.Body = body;
    
    // Act - testing the code that actually does the work
    await HealthCheckHighMark.ResponseWriter(context, report);
    
    //Assert
    //... read JSON from stream 
    body.Seek(0, SeekOrigin.Begin);
    var reader = new StreamReader(body);
    string json = reader.ReadToEnd();

    //... and assert desired behavior
}
[事实]
公共异步任务TestHighMarkResponse(){
//安排
HttpContext=新的DefaultHttpContext();
var report=Mock.Of();
流体=新的MemoryStream();
context.Response.Body=Body;
//Act-测试实际执行工作的代码
等待HealthCheckHighMark.ResponseWriter(上下文、报告);
//断言
//…从流中读取JSON
body.Seek(0,SeekOrigin.Begin);
变量读取器=新的流读取器(主体);
字符串json=reader.ReadToEnd();
//…并断言期望的行为
}

它只是一个包含3个返回值的方法的类。在这种情况下,
GetHighMark()
方法返回
HighMark
的值。对提供的解决方案有任何反馈吗?