Ejemplo n.º 1
0
        public virtual void OnDelete(BookModel book)
        {
            if (book == null)
            {
                return;
            }

            using (var context = new BookContext())
            {
                LibraryDb = new BookLibraryDb(context);
                LibraryDb.DeleteBook(book.BookId);
            }
        }
Ejemplo n.º 2
0
        public void DeleteBook_DeletesBook_ViaContextTest()
        {
            //--Arrange
            var mockSet = new Mock <DbSet <BookModel> >();
            var data    = Data.AsQueryable();

            mockSet.As <IQueryable <BookModel> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <BookModel> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <BookModel> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <BookModel> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
            var mockContext = new Mock <BookContext>();
            var library     = new BookLibraryDb(mockContext.Object);

            //--Act
            mockContext.Setup(m => m.Books).Returns(mockSet.Object);
            library.DeleteBook(9999);

            //--Assert
            mockSet.Verify(m => m.Remove(It.IsAny <BookModel>()), Times.Once);
            mockContext.Verify(m => m.SaveChanges(), Times.Once);
        }