コード例 #1
0
ファイル: BookServiceTests.cs プロジェクト: Indexu/LibraryAPI
        public void UpdateBookEmpty()
        {
            // Arrange
            int bookID         = 1;
            var patchViewModel = new PatchBookViewModel();
            var viewModel      = MockDataGenerator.CreateBookViewModel(bookID);
            var oldDTO         = MockDataGenerator.CreateBook(bookID);

            var mockBookRepo = new Mock <IBookRepository>();
            var mockLoanRepo = new Mock <ILoanRepository>();
            var mockMapper   = new Mock <IMapper>();

            mockBookRepo.Setup(f => f.GetBookByID(bookID)).Returns(oldDTO);
            mockBookRepo.Setup(f => f.UpdateBook(bookID, viewModel));
            mockMapper.Setup(x => x.Map <PatchBookViewModel, BookViewModel>(patchViewModel)).Returns(viewModel);

            var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object);

            // Act
            service.UpdateBook(bookID, patchViewModel);

            // Assert
            mockBookRepo.Verify(f => f.GetBookByID(bookID), Times.Once());
            mockBookRepo.Verify(f => f.UpdateBook(bookID, viewModel), Times.Once());
            mockMapper.Verify(f => f.Map <PatchBookViewModel, BookViewModel>(patchViewModel), Times.Once());
        }
コード例 #2
0
ファイル: BookServiceTests.cs プロジェクト: Indexu/LibraryAPI
        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);
        }
コード例 #3
0
        public void AddBook()
        {
            // Arrange
            var bookID            = 11;
            var entites           = MockDataGenerator.CreateBookEntities(10);
            var entitiesQueryable = entites.AsQueryable();
            var viewModel         = MockDataGenerator.CreateBookViewModel(bookID);
            var BookEntity        = MockDataGenerator.CreateBookEntity(bookID);
            var BookDTO           = MockDataGenerator.CreateBook(bookID);

            var mockDbContext = new Mock <DatabaseContext>();
            var mockBookSet   = new Mock <DbSet <BookEntity> >();
            var mockMapper    = new Mock <IMapper>();

            mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Provider).Returns(entitiesQueryable.Provider);
            mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Expression).Returns(entitiesQueryable.Expression);
            mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.ElementType).Returns(entitiesQueryable.ElementType);
            mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator()); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator());
            mockBookSet.Setup(f => f.Add(It.Is <BookEntity>(data => data.Title == BookEntity.Title))).Callback <BookEntity>(u => u.ID = BookEntity.ID);
            mockDbContext.Setup(f => f.Books).Returns(mockBookSet.Object);
            mockMapper.Setup(f => f.Map <BookViewModel, BookEntity>(It.Is <BookViewModel>(data => data.Title == viewModel.Title))).Returns(BookEntity);
            mockMapper.Setup(f => f.Map <BookEntity, BookDTO>(It.Is <BookEntity>(data => data.ID == BookDTO.ID))).Returns(BookDTO);

            var repository = new BookRepository(mockDbContext.Object, mockMapper.Object);

            // Act
            var result = repository.AddBook(viewModel);

            // Assert
            mockMapper.Verify(f => f.Map <BookViewModel, BookEntity>(It.Is <BookViewModel>(data => data.Title == viewModel.Title)), Times.Once());
            mockMapper.Verify(f => f.Map <BookEntity, BookDTO>(It.Is <BookEntity>(data => data.ID == BookDTO.ID)), Times.Once());
            mockBookSet.Verify(f => f.Add(It.Is <BookEntity>(data => data.Title == BookEntity.Title)), Times.Once());
            Assert.IsNotNull(result);
            Assert.AreEqual(result.ID, bookID);
        }
コード例 #4
0
ファイル: BookServiceTests.cs プロジェクト: Indexu/LibraryAPI
        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);
        }
コード例 #5
0
ファイル: BookServiceTests.cs プロジェクト: Indexu/LibraryAPI
        public void AddBook()
        {
            // Arrange
            int bookID = 1;

            var bookViewModel = MockDataGenerator.CreateBookViewModel(bookID);
            var bookDTO       = MockDataGenerator.CreateBook(bookID);

            var mockBookRepo = new Mock <IBookRepository>();
            var mockLoanRepo = new Mock <ILoanRepository>();
            var mockMapper   = new Mock <IMapper>();

            mockBookRepo.Setup(f => f.AddBook(bookViewModel)).Returns(bookDTO);

            var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object);

            // Act
            var returnedBook = service.AddBook(bookViewModel);

            // Assert
            mockBookRepo.Verify(f => f.AddBook(bookViewModel), Times.Once());
            Assert.IsNotNull(returnedBook);
            Assert.AreEqual(returnedBook.Title, bookDTO.Title);
        }