Esempio n. 1
0
        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());
        }
Esempio n. 2
0
        public void UpdateBookException()
        {
            // Arrange
            var bookID            = 5;
            var entites           = MockDataGenerator.CreateBookEntities(10);
            var entitiesQueryable = entites.AsQueryable();
            var viewModel         = MockDataGenerator.CreateBookViewModel(20);
            var BookEntity        = MockDataGenerator.CreateBookEntity(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.Update(It.Is <BookEntity>(data => data.ID == BookEntity.ID))).Throws(new Exception());
            mockDbContext.Setup(f => f.Books).Returns(mockBookSet.Object);

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

            // Act
            repository.UpdateBook(bookID, viewModel);
        }
Esempio n. 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);
        }
Esempio n. 4
0
        public void ReplaceBookException()
        {
            // Arrange
            int bookID    = 1;
            var viewModel = MockDataGenerator.CreateBookViewModel(bookID);

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

            mockBookRepo.Setup(f => f.UpdateBook(bookID, viewModel)).Throws(new Exception());

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

            // Act
            service.ReplaceBook(bookID, viewModel);
        }
Esempio n. 5
0
        public void UpdateBookExceptionGet()
        {
            // Arrange
            int bookID         = 1;
            var patchViewModel = MockDataGenerator.CreatePatchBookViewModel(bookID);
            var viewModel      = MockDataGenerator.CreateBookViewModel(bookID);

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

            mockBookRepo.Setup(f => f.GetBookByID(bookID)).Throws(new Exception());
            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);
        }
Esempio n. 6
0
        public void AddBookAlreadyExists()
        {
            // Arrange
            var bookID            = 5;
            var entites           = MockDataGenerator.CreateBookEntities(10);
            var entitiesQueryable = entites.AsQueryable();
            var viewModel         = MockDataGenerator.CreateBookViewModel(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());
            mockDbContext.Setup(f => f.Books).Returns(mockBookSet.Object);

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

            // Act
            repository.AddBook(viewModel);
        }
Esempio n. 7
0
        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);
        }