gpt4 book ai didi

c# - 如何实例化 ResourceResponse

转载 作者:太空狗 更新时间:2023-10-29 19:38:37 24 4
gpt4 key购买 nike

我正在尝试模拟一个返回 ResourceResponse<Document> 的调用,但我无法实例化该类型。是否有可以实例化它的工厂类或其他方式?

编辑

var response = new ResourceResponse<Document>();

The type 'Microsoft.Azure.Documents.Client.ResourceResponse' has no constructors defined

最佳答案

最新稳定版 Microsoft.Azure.DocumentDB (1.10.0) atm 添加了 2 个构造函数用于模拟目的。

https://msdn.microsoft.com/en-us/library/azure/dn799209.aspx#Anchor_2

编辑

使用 Moq 你可以做这样的事情:

Mock<IDocumentClient> documentClient = new Mock<IDocumentClient>();
documentClient
.Setup(dc => dc.ReplaceDocumentAsync(UriFactory.CreateDocumentUri("database", "collection", "id"), object, null) // last parameter are RequestOptions, these are null by default
.Returns(Task.FromResult(new ResourceResponse<Document>()));

这样我可以检查我的 documentClient 上的方法是否被调用,如果你想影响文档中返回的内容,你必须创建一个文档,然后是该文档的 ResourceResponse。像这样的东西:

var document = new Document();
document.LoadFrom(jsonReader); // the json reader should contain the json of the document you want to return
Mock<IDocumentClient> documentClient = new Mock<IDocumentClient>();
documentClient
.Setup(dc => dc.ReplaceDocumentAsync(UriFactory.CreateDocumentUri("database", "collection", "id"), object, null) // last parameter are RequestOptions, these are null by default
.Returns(Task.FromResult(new ResourceResponse<Document>(document)));

关于c# - 如何实例化 ResourceResponse<Document>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262541/

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