[C#] DefaultHttpContext.Response is null, why?
Hi :) I have been learning how to create an api and I did an Global Exceptions Handler. After that i have done unit tests for this. I did this (not copy-paste) but i wrote line by line how it was on this website http://anthonygiretti.com/2018/09/04/asp-net-core-2-1-middlewares-part2-unit-test-a-custom-middleware/ and my context.Response.Body is null because context.Response is null. I tried to find sth in the internet but i couldn't :( could somebody help me? private DefaultHttpContext GetHttpContext() { var context = new DefaultHttpContext(); context.Response.Body = new MemoryStream(); return context; } [Fact] public async void A() { //Arrange var middleware = new ExceptionMiddleware((HttpContext innerHttpContext) => { throw new FakeApiException(); }); var context = GetHttpContext(); //Act await middleware.InvokeAsync(context); context.Response.Body.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(context.Response.Body); string streamText = await reader.ReadToEndAsync(); ErrorDetails objResponse = JsonConvert.DeserializeObject<ErrorDetails>(streamText); //Assert Assert.Equal("Internal Server Error from the custom middleware", objResponse.Message); } I have been larning english and can you tell me, where (if they are exists) mistakes? Thanks in advance :)