Esempio n. 1
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. 2
0
        public void DeleteBook()
        {
            // Arrange
            var bookID            = 5;
            var entites           = MockDataGenerator.CreateBookEntities(10);
            var entitiesQueryable = entites.AsQueryable();
            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.Remove(It.Is <BookEntity>(data => data.ID == BookEntity.ID)));
            mockDbContext.Setup(f => f.Books).Returns(mockBookSet.Object);

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

            // Act
            repository.DeleteBookByID(bookID);

            // Assert
            mockBookSet.Verify(f => f.Remove(It.Is <BookEntity>(data => data.ID == BookEntity.ID)), Times.Once());
        }
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);
        }