public async Task DeleteBookForAuthorAsync_AuthorNotExist_Test() { // Arrange var authorId = Guid.NewGuid(); var bookId = Guid.NewGuid(); var mockRepo = new Mock <ILibraryRepository>(); mockRepo.Setup(repo => repo.AuthorExistsAsync(authorId)) .ReturnsAsync(false); var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new BookProfile())).CreateMapper(); var controller = new BooksController(mockRepo.Object, mapper); // Act var result = await controller.DeleteBookForAuthorAsync(authorId, bookId); // Assert Assert.IsType <NotFoundResult>(result); }
public async Task DeleteBookForAuthorAsync_ThrowException_Test() { // Arrange var authorId = Guid.NewGuid(); var bookId = Guid.NewGuid(); var mockRepo = new Mock <ILibraryRepository>(); mockRepo.Setup(repo => repo.AuthorExistsAsync(authorId)) .Returns(Task.FromResult(true)); mockRepo.Setup(repo => repo.GetBookForAuthorAsync(authorId, bookId)) .ReturnsAsync(new Book()); mockRepo.Setup(repo => repo.SaveChangesAsync()) .ReturnsAsync(false); var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new BookProfile())).CreateMapper(); var controller = new BooksController(mockRepo.Object, mapper); // Act var result = await Assert.ThrowsAsync <Exception>(() => controller.DeleteBookForAuthorAsync(authorId, bookId)); // Assert Assert.Equal($"Deleting book {bookId} for author {authorId} failed on save.", result.Message); }