public void GetBookByIDExceptionBookRepo() { // Arrange var pageNumber = 1; int?pageSize = null; int numberOfLoans = 10; int bookID = 1; var bookDTO = MockDataGenerator.CreateBook(bookID); var bookDetailsBasicDTO = MockDataGenerator.CreateBasicBookDetails(bookID); var bookLoans = MockDataGenerator.CreateBookLoans(numberOfLoans); var envelope = MockDataGenerator.CreateBookLoansEnvelope(bookLoans, pageNumber, pageSize); var bookDetailsDTO = MockDataGenerator.CreateBasicBookDetails(bookID); bookDetailsDTO.LoanHistory = envelope; var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockBookRepo.Setup(f => f.GetBookByID(bookID)).Throws(new Exception()); mockLoanRepo.Setup(f => f.GetLoansByBookID(bookID, false, pageNumber, pageSize)).Returns(envelope); mockMapper.Setup(x => x.Map <BookDTO, BookDetailsDTO>(bookDTO)).Returns(bookDetailsBasicDTO); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.GetBookByID(bookID, pageNumber, pageSize); }
public void GetBookByIDBasic() { // Arrange var pageNumber = 1; int?pageSize = null; int numberOfLoans = 10; int bookID = 1; var bookDTO = MockDataGenerator.CreateBook(bookID); var bookDetailsBasicDTO = MockDataGenerator.CreateBasicBookDetails(bookID); var bookLoans = MockDataGenerator.CreateBookLoans(numberOfLoans); var envelope = MockDataGenerator.CreateBookLoansEnvelope(bookLoans, pageNumber, pageSize); var bookDetailsDTO = MockDataGenerator.CreateBasicBookDetails(bookID); bookDetailsDTO.LoanHistory = envelope; var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockBookRepo.Setup(f => f.GetBookByID(bookID)).Returns(bookDTO); mockLoanRepo.Setup(f => f.GetLoansByBookID(bookID, false, pageNumber, pageSize)).Returns(envelope); mockMapper.Setup(x => x.Map <BookDTO, BookDetailsDTO>(bookDTO)).Returns(bookDetailsBasicDTO); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act var returnedBook = service.GetBookByID(bookID, pageNumber, pageSize); // Assert mockBookRepo.Verify(f => f.GetBookByID(bookID), Times.Once()); mockLoanRepo.Verify(f => f.GetLoansByBookID(bookID, false, pageNumber, pageSize), Times.Once()); mockMapper.Verify(f => f.Map <BookDTO, BookDetailsDTO>(bookDTO), Times.Once()); Assert.IsNotNull(returnedBook); Assert.AreEqual(returnedBook.ID, bookID); Assert.AreEqual(returnedBook.LoanHistory.Paging.PageNumber, pageNumber); Assert.AreEqual(returnedBook.LoanHistory.Paging.PageMaxSize, 50); Assert.AreEqual(returnedBook.LoanHistory.Paging.PageCount, 1); Assert.AreEqual(returnedBook.LoanHistory.Paging.TotalNumberOfItems, numberOfLoans); Assert.AreEqual(returnedBook.LoanHistory.Items.First().User.ID, bookLoans.First().User.ID); Assert.AreEqual(returnedBook.LoanHistory.Items.Last().User.ID, bookLoans.Last().User.ID); }