public async Task AlbumsAndPhotosForUserControllerActionDoesNotReturnOkIfExceptionIsRaised() { // Arrange var albumsAndPhotosServiceMock = new Mock <IAlbumsAndPhotosService>(); albumsAndPhotosServiceMock.Setup(x => x.GetAlbumsAndPhotosForUserAsync(It.IsAny <int>())).Throws <Exception>(); var albumsAndPhotosController = new AlbumsAndPhotosController(albumsAndPhotosServiceMock.Object); // Act var result = await albumsAndPhotosController.GetAlbumsAndPhotosForUser(It.IsAny <int>()); // Assert Assert.IsInstanceOf(typeof(ObjectResult), result); Assert.IsNotInstanceOf(typeof(OkObjectResult), result); }
public async Task AlbumsAndPhotosForUserControllerActionReturnsOkResultWithWhatTheAlbumsAndPhotosServiceReturnsOnlyForTheUser([Random(1, 100, 50)] int randomUserID) { // Arrange var albumAndPhotoForTest = new AlbumAndPhoto(randomUserID, 1, "", 1, "", "", ""); var albumsAndPhotosServiceMock = new Mock <IAlbumsAndPhotosService>(); albumsAndPhotosServiceMock.Setup(x => x.GetAlbumsAndPhotosForUserAsync(2)) .Returns(Task.FromResult(result: new List <AlbumAndPhoto>() { albumAndPhotoForTest } as IEnumerable <AlbumAndPhoto>)); var albumsAndPhotosController = new AlbumsAndPhotosController(albumsAndPhotosServiceMock.Object); // Act var result = await albumsAndPhotosController.GetAlbumsAndPhotosForUser(2); // Assert Assert.IsInstanceOf(typeof(OkObjectResult), result); Assert.IsInstanceOf(typeof(IEnumerable <AlbumAndPhoto>), (result as OkObjectResult).Value); Assert.AreEqual(((result as OkObjectResult).Value as IEnumerable <AlbumAndPhoto>).FirstOrDefault(), albumAndPhotoForTest); Assert.AreEqual(((result as OkObjectResult).Value as IEnumerable <AlbumAndPhoto>).All(x => x.UserID == randomUserID), true); }