gpt4 book ai didi

linq - 无法转换类型为 'System.Linq.EnumerableQuery to type ' Microsoft.Azure.Documents.Linq.IDocumentQuery 的对象

转载 作者:行者123 更新时间:2023-12-02 01:30:32 35 4
gpt4 key购买 nike

我有一个具有以下方法的类,并使用 Moq 作为单元测试框架。我如何模拟以下内容:

FeedOptions feedOptions = new FeedOptions
{
MaxItemCount = 1000
};

var query = await _storeAccessClient.CreateDocumentQueryAsync<CustomEntity>(_collectionLink, feedOptions)
.Where(c => c.DataType == _dataType)
.OrderBy(c => c.StartTime, sortOrder)
.AsDocumentQuery()
.ExecuteNextAsync<CustomEntity>();

List<CustomEntity> result = query.ToList<CustomEntity>();

非常感谢任何帮助!!

最佳答案

您所要做的就是创建一个围绕 EnumerableQuery 类的包装器,该类继承自 IQueryable 和 IDocumentQuery,如下所示:

internal class MockEnumerableQuery : IDocumentQuery<JTokenEx>, IOrderedQueryable<JTokenEx>
{
public IQueryable<JTokenEx> List;
private readonly bool bypassExpressions;


public MockEnumerableQuery(EnumerableQuery<JTokenEx> List, bool bypassExpressions = true)
{
this.List = List;
this.bypassExpressions = bypassExpressions;
}


public IEnumerator<JTokenEx> GetEnumerator()
{
return List.GetEnumerator();
}


IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}


public Expression Expression => List.Expression;

public Type ElementType => typeof(JTokenEx);


public IQueryProvider Provider => new MockQueryProvider(this, bypassExpressions);


public void Dispose()
{
}


public Task<FeedResponse<TResult>> ExecuteNextAsync<TResult>(CancellationToken token = new CancellationToken())
{
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
FeedResponse<JToken> feed = Activator.CreateInstance(typeof(FeedResponse<JToken>),
flags,null,new Object[] { List.Select(j => (JToken) j), 0, new NameValueCollection(), false, null}, null)
as FeedResponse<JToken>;

return Task.FromResult(feed as FeedResponse<TResult>);
}


public Task<FeedResponse<dynamic>> ExecuteNextAsync(CancellationToken token = new CancellationToken())
{
throw new NotImplementedException();
}


public bool HasMoreResults { get; }
}


class MockQueryProvider : IQueryProvider
{
private readonly MockEnumerableQuery mockQuery;
private readonly bool bypassExpressions;

public MockQueryProvider(MockEnumerableQuery mockQuery, bool byPassExpressions)
{
this.mockQuery = mockQuery;
this.bypassExpressions = byPassExpressions;
}


public IQueryable CreateQuery(Expression expression)
{
throw new NotImplementedException();
}


public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
if (!bypassExpressions)
{
mockQuery.List = mockQuery.List.Provider.CreateQuery<TElement>(expression) as IQueryable<JTokenEx>;
}

return (IQueryable<TElement>)mockQuery;
}


public object Execute(Expression expression)
{
throw new NotImplementedException();
}


public TResult Execute<TResult>(Expression expression)
{
throw new NotImplementedException();
}
}

现在你的模拟返回 EnumerableQuery,你返回这个 MockEnumerableQuery 类,你应该很好。

关于linq - 无法转换类型为 'System.Linq.EnumerableQuery to type ' Microsoft.Azure.Documents.Linq.IDocumentQuery 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563690/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com