public async Task VideoById_Should_Call_IVideoService_GetVideoById_And_Return_NotFound_If_IVideoService_GetVideoById_Returns_Null() { VideoControllerMock mock = VideoControllerMock.Create(); mock.VideoService.Setup(service => service.GetVideoById(It.IsAny <int>())).ReturnsAsync(() => null); IActionResult actionResult = await mock.VideoById(1); var notFoundResult = actionResult as NotFoundResult; Assert.NotNull(notFoundResult); mock.VideoService.Verify(service => service.GetVideoById(It.IsAny <int>()), Times.Once); }
public async Task VideoById_Should_Return_BadRequest_If_ModelState_Is_Invalid() { VideoControllerMock mock = VideoControllerMock.Create(); mock.ModelState.AddModelError("test", "test"); IActionResult actionResult = await mock.VideoById(1); var badRequestObjectResult = actionResult as BadRequestObjectResult; Assert.NotNull(badRequestObjectResult); var serializableError = badRequestObjectResult.Value as SerializableError; Assert.NotNull(serializableError); Assert.True(((string[])serializableError["test"])[0] == "test"); mock.VideoService.Verify(service => service.GetVideoById(It.IsAny <int>()), Times.Never); }
public async Task VideoById_Should_Call_IVideoService_GetVideoById_And_Return_Ok() { VideoControllerMock mock = VideoControllerMock.Create(); var id = 1; mock.VideoService.Setup(service => service.GetVideoById(It.Is <int>(i => i == id))).ReturnsAsync(() => new VideoModel()); IActionResult actionResult = await mock.VideoById(id); var okObjectResult = actionResult as OkObjectResult; Assert.NotNull(okObjectResult); var videoModel = okObjectResult.Value as VideoModel; Assert.NotNull(videoModel); mock.VideoService.Verify(service => service.GetVideoById(It.IsAny <int>()), Times.Once); }