如何实例化ResourceResponse <Document>

Jon*_*ski 8 c# moq azure-cosmosdb

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

编辑

var response = new ResourceResponse<Document>();

类型'Microsoft.Azure.Documents.Client.ResourceResponse'没有定义构造函数

Yen*_*heO 6

最新的稳定版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>()));
Run Code Online (Sandbox Code Playgroud)

这样我可以检查我的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)));
Run Code Online (Sandbox Code Playgroud)

  • 如果你想要一个初始化jsonreader的例子,我可以鞭打一个. (2认同)
  • 你可以实例化 `ResourceResponse`,但你不能改变 `.StatusCode` 属性(或任何其他属性),因为它只有一个 getter。当代码试图访问它时,它总是抛出一个 `NullReferenceException`。 (2认同)