如何使用DocumentDb单元测试的筛选器模拟DocumentClient CreateDocumentQuery?

use*_*348 5 c# unit-testing documentclient

我正在为下面的代码编写单元测试,这些代码使用带有where(filter)条件的Document客户端CreateDocumentQuery方法。

 var documentQuery = ReadOnlyDocumentClient.CreateDocumentQuery<MyView>(UriFactory.CreateDocumentCollectionUri(Constants.COSMOS_DB_NAME, Constants.MY_VIEW_COL_NAME))
                                                       .Where(w => w.Name== query.Name)
                                                       .Where(w => w.DOB> query.StartDate && w.DOB < query.EndDate)
                                                       .AsDocumentQuery();
Run Code Online (Sandbox Code Playgroud)

查询是一个命令参数,它是一个输入。

我正在使用最小起订量进行模拟,并且正在使用以下代码行进行模拟

 mockDocumentClient.Setup(s => s.CreateDocumentQuery<MyView>(It.IsAny<Uri>(), It.IsAny<FeedOptions>())).Returns(mockDocumentQuery.Object);
Run Code Online (Sandbox Code Playgroud)

现在,上面的代码仅模拟CreateDocumentQuery,执行条件时,我没有得到模拟支持。

请提出如何在哪里条件下模拟CreateDocumentQuery?

提前致谢。

M. *_*huk 4

当您模拟 时CreateDocumentQuery,您还需要模拟IQueryProviderIEnumerable您期望作为查询结果的 进行交互。

这是一个例子:

        // somewhere in your test class
        public interface IFakeDocumentQuery<T> : IDocumentQuery<T>, IOrderedQueryable<T>
        {
        }

        // somewhere in your test method
        var expected = new List<YourType>
        {
            new YourType
            {
                yourField = "yourValue"
            }
        };

        var mockDocumentClient = new Mock<IDocumentClient>();
        var dataSource = expected.AsQueryable();
        var response = new FeedResponse<YourType>(dataSource);
        var mockDocumentQuery = new Mock<IFakeDocumentQuery<YourType>>();

        // the part that gets the work done :)
        var provider = new Mock<IQueryProvider>();
        provider
            .Setup(p => p.CreateQuery<YourType>(It.IsAny<Expression>()))
            .Returns(mockDocumentQuery.Object);
        mockDocumentQuery
            .Setup(q => q.ExecuteNextAsync<YourType>(It.IsAny<CancellationToken>()))
            .ReturnsAsync(response);
        mockDocumentQuery
            .SetupSequence(q => q.HasMoreResults)
            .Returns(true)
            .Returns(false);
        mockDocumentQuery
            .As<IQueryable<YourType>>()
            .Setup(x => x.Provider)
            .Returns(provider.Object);
        mockDocumentQuery
            .As<IQueryable<YourType>>()
            .Setup(x => x.Expression)
            .Returns(dataSource.Expression);
        mockDocumentQuery
            .As<IQueryable<YourType>>()
            .Setup(x => x.ElementType)
            .Returns(dataSource.ElementType);
        mockDocumentQuery
            .As<IQueryable<YourType>>()
            .Setup(x => x.GetEnumerator())
            .Returns(dataSource.GetEnumerator);
        mockDocumentClient
            .Setup(c => c.CreateDocumentQuery<YourType>(It.IsAny<Uri>(), It.IsAny<FeedOptions>()))
            .Returns(mockDocumentQuery.Object);
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!:)