public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var id = (int)context.ActionArguments["id"]; var jogging = await _repo.GetJoggingById(id); if (jogging == null) { _logger.LogInfo($"Jogging with id: {id} doesn't exist in the database."); context.Result = new NotFoundResult(); } else { context.HttpContext.Items.Add("jogging", jogging); await next(); } }
public async Task GetJoggingById_ReturnsOkObjectResult() { // Arrange var joggingId = 1000; var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "adminuser"), new Claim(ClaimTypes.Role, "Admin") })); var jogging = await _joggingRepo.GetJoggingById(joggingId); _controller.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext { User = user } }; _controller.ControllerContext.HttpContext.Items.Add("jogging", jogging); // Act var result = await _controller.GetJoggingById(joggingId); // Assert Assert.IsType <OkObjectResult>(result); }