public virtual void OnDelete(BookModel book) { if (book == null) { return; } using (var context = new BookContext()) { LibraryDb = new BookLibraryDb(context); LibraryDb.DeleteBook(book.BookId); } }
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); }