Exemplo n.º 1
0
        public async void Test4GetAsync()
        {
            var mockClient     = new Mock <IMongoClient>();
            var mockContext    = new Mock <IMongoContext>();
            var client         = mockClient.Object;
            var mockCollection = CreateMockCollection();
            var collection     = mockCollection.Object;
            var session        = new Mock <IClientSessionHandle>().Object;

            var options           = new ChangeStreamOptions();
            var cancellationToken = new CancellationTokenSource().Token;

            Book document = new Book();

            document.Author   = "asdas";
            document.BookName = "sadasd";

            document.Id = ObjectId.GenerateNewId().ToString();

            collection.InsertOne(document);
            var bookMock = new Mock <IFindFluent <Book, Book> >();

            mockContext.Setup(x => x.GetCollection <Book>("Book")).Returns(mockCollection.Object);


            BookstoreDatabaseSettings setting = new BookstoreDatabaseSettings();

            setting.BooksCollectionName = "Book";
            var mockIDBsettings = new Mock <IOptions <BookstoreDatabaseSettings> >();

            mockIDBsettings.Setup(x => x.Value).Returns(setting);

            //mockCollection.Setup(x => x.FindAsync(It.IsAny<ExpressionFilterDefinition<Book>>(), null, cancellationToken))
            //    .Returns(bookMock.Object);

            BookService service = new BookService(mockIDBsettings.Object, mockContext.Object);
            //service.Create(document);
            var result = await service.GetBooksAsync();

            mockContext.Verify(x => x.GetCollection <Book>("Book"), Times.Once);



            var options1 = new FindOptions <Book>(); // no projection

            // var filterExpression = (Expression<Func<Book, bool>>)(x => x.BookName == "sadasd");

            var filter  = It.IsAny <ExpressionFilterDefinition <Book> >();
            var filter1 = It.IsAny <FilterDefinition <Book> >();
            //var filter2 = It.IsAny<EmptyFilterDefinition<Book>>();
            //FindFluent<Book, Book> fluent;
            //
            //mockCollection.Verify(x=>x.Find(filterExpression, null), Times.Once);
            //
            //var filterDefinition = Builders<Book>.Filter.Eq("BookName", "sadasd");


            var filterExpression = (Expression <Func <Book, bool> >)(x => x.BookName == "sadasd");

            IMongoCollectionExtensions.FindAsync(collection, session, filterExpression, options1, cancellationToken);
            mockCollection.Verify(m => m.FindAsync <Book>(session, It.IsAny <ExpressionFilterDefinition <Book> >(), options1, cancellationToken), Times.Once);
            mockCollection.Verify(m => m.FindAsync <Book>(FilterDefinition <Book> .Empty, null, CancellationToken.None), Times.Once);
            //
            //
            //
            //
            //
        }