Пример #1
0
        public void MarkBookAsRead_MarkAsReadByBookId_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.MarkBookAsRead(9999);

            //--Assert
            mockContext.Verify(m => m.SaveChanges(), Times.Once);
            Assert.IsTrue(library.GetBook(9999).IsRead);
        }
Пример #2
0
        public void GetBook_ReturnsBookByBookId_ViaContextTests()
        {
            //--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);

            var expected = Data[0]; // Hobbit

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

            //--Assert
            Assert.AreEqual(expected.Author, actual.Author);
        }