This week I learned more about the Net Core Request Pipeline.
Especially, that when using a controller with non default ActionResult return type, like for example BadRequest(), that these ActionResults will get wrapped in another ActionResult, which makes unit testing kind of tedious. E.g.:
// controller method[Route("{id:int}")][HttpGet][ProducesResponseType(StatusCodes.Status200OK)][ProducesResponseType(StatusCodes.Status404NotFound)]publicasyncTask<ActionResult<Author>>GetOneById(intid){Authorresult=await_authorService.GetById(id);if(result!=null){returnresult;}else{returnNotFound();}}// tests[Fact(DisplayName="Should return correct author by id")]publicasyncTaskShould_Return_Author_By_Id(){// arrangevarexpected=fixture.authorList.Where(author=>author.Id==1).FirstOrDefault();// actvarresult=awaitcontroller.GetOneById(1);// assertAssert.IsType<ActionResult<Author>>(result);Assert.Equal(expected,result.Value);// Using Value since ReturnType is ActionResult<Author>, so we need to access the value}[Fact(DisplayName="Should return NotFound()")]publicasyncTaskShould_Return_Author_NotFound(){// actvarresult=awaitcontroller.GetOneById(999);// assertAssert.Null(result.Value);Assert.IsType<ActionResult<Author>>(result);// check method return type matchesAssert.IsType<NotFoundResult>(result.Result);// check actual return is as expected}
I'm a fan of Open Source and have a growing interest in serverless and edge computing. I'm not a big fan of spiders, but they're doing good work eating bugs. I also stream on Twitch.
This week I learned more about the Net Core Request Pipeline.
Especially, that when using a controller with non default ActionResult return type, like for example BadRequest(), that these ActionResults will get wrapped in another ActionResult, which makes unit testing kind of tedious. E.g.:
Mario approves. He's just not sure which pipeline to jump into 😉