Пример #1
0
        public void DocumentTypeService_LookupAsync_ReturnsNoRecordsIfNotActive()
        {
            // Arrange
            var mockDbContextScopeFac = new Mock<IDbContextScopeFactory>();
            var mockDbContextScope = new Mock<IDbContextReadOnlyScope>();
            var mockEfDbContext = new Mock<EFDbContext>();
            mockDbContextScopeFac.Setup(x => x.CreateReadOnly(DbContextScopeOption.JoinExisting)).Returns(mockDbContextScope.Object);
            mockDbContextScope.Setup(x => x.DbContexts.Get<EFDbContext>()).Returns(mockEfDbContext.Object);

            var dbEntry1 = new DocumentType { Id = "dummyEntryId1", DocTypeName = "Name1", DocTypeAltName = "NameAlt1", IsActive_bl = true };
            var dbEntry2 = new DocumentType { Id = "dummyEntryId2", DocTypeName = "Name2", DocTypeAltName = "NameAlt2", IsActive_bl = false };
            var dbEntry3 = new DocumentType { Id = "dummyEntryId3", DocTypeName = "Name3", DocTypeAltName = "NameAlt3", IsActive_bl = true };
            var dbEntries = (new List<DocumentType> { dbEntry1, dbEntry2, dbEntry3 }).AsQueryable();

            var mockDbSet = new Mock<DbSet<DocumentType>>();
            mockDbSet.As<IDbAsyncEnumerable<DocumentType>>().Setup(m => m.GetAsyncEnumerator()).Returns(new MockDbAsyncEnumerator<DocumentType>(dbEntries.GetEnumerator()));
            mockDbSet.As<IQueryable<DocumentType>>().Setup(m => m.Provider).Returns(new MockDbAsyncQueryProvider<DocumentType>(dbEntries.Provider));
            mockDbSet.As<IQueryable<DocumentType>>().Setup(m => m.Expression).Returns(dbEntries.Expression);
            mockDbSet.As<IQueryable<DocumentType>>().Setup(m => m.ElementType).Returns(dbEntries.ElementType);
            mockDbSet.As<IQueryable<DocumentType>>().Setup(m => m.GetEnumerator()).Returns(dbEntries.GetEnumerator());
            mockDbSet.Setup(x => x.Include(It.IsAny<string>())).Returns(mockDbSet.Object);

            mockEfDbContext.Setup(x => x.DocumentTypes).Returns(mockDbSet.Object);

            var docTypeService = new DocumentTypeService(mockDbContextScopeFac.Object, "dummyUserId");

            //Act
            var returnedDocTypes = docTypeService.LookupAsync("Name2", true).Result;

            //Assert
            Assert.IsTrue(returnedDocTypes.Count == 0);
        }