7

我正在为下面的代码编写单元测试,该代码使用带有 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();

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

我正在使用 MOQ 来模拟,我正在使用下面的代码行来模拟

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

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

请建议我如何在 where 条件下模拟 CreateDocumentQuery?

提前致谢。

4

2 回答 2

4

当您模拟 时CreateDocumentQuery,您还需要模拟与您期望的查询结果IQueryProvider交互的。IEnumerable

这是一个例子:

        // 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);

我希望这会有所帮助!:)

于 2019-07-25T14:25:39.300 回答
3

您可以尝试以下示例:

var documentClientMock = new Mock<IDocumentClient>();
var docs = new List<MyView>();//your mock data
documentClientMock.Setup(d => d.CreateDocumentQuery<MyView>(It.IsAny<Uri>(), It.IsAny<FeedOptions>()))
    .Returns((IOrderedQueryable<MyView>) docs.AsQueryable());
于 2019-07-25T15:18:01.243 回答