How to create an instance of ResourceResponse <Document>
I am trying to make fun of the call that ResourceResponse<Document> returns, but I cannot create an instance of this type. Is there a factory class that can instantiate or some other way to do this?
EDIT
var response = new ResourceResponse<Document>();
The type "Microsoft.Azure.Documents.Client.ResourceResponse" has no specific constructors
The latest stable version of Microsoft.Azure.DocumentDB (1.10.0) atm adds 2 constructors for mocking purposes.
https://msdn.microsoft.com/en-us/library/azure/dn799209.aspx#Anchor_2
edit
Using Moq, you can do something like this:
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>())); Thus, I can check if the method is called in my documentClient, if you want to influence what is returned in the document, you must create the document and then ResourceResponse of this document. Sort of:
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))); Pretty late, but with Microsoft.Azure.DocumentDB.Core 2.4.2 there was a DocumentClient constructor that takes an HttpMessageHandler as a parameter. This is ugly (so much that it deserves a negative answer), but you can use this to add answers with status codes. As far as I can say nothing, leaving my box when I do this.
I also had to create a constructor for testing only, since I would never want to use this at runtime. But for those who are still interested, until the best library comes out, there are relevant parts.
Create your customer
var client = new DocumentClient(host, authText, handler); In your tests, you can use a mock handler like this:
public static Mock<HttpMessageHandler> CreateHttpMessageHandler(List<HttpResponseMessage> responses) { var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); handlerMock.Protected() .Setup<Task<HttpResponseMessage>>( nameof(HttpClient.SendAsync), ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .ReturnsAsync(() => { var response = responses[0]; responses.RemoveAt(0); return response; }) .Verifiable(); return handlerMock; } I created this answer factory simulator
private HttpResponseMessage Respond(string text) { var response = new HttpResponseMessage(HttpStatusCode.OK) { StatusCode = HttpStatusCode.OK, Content = new StringContent(text), }; return response; } and configure the handler something like this:
var responses = new List<HttpResponseMessage> { Respond("{ ... }"), Respond("{ ... }"), Respond("{ ... }"), Respond("{ ... }"), }; Replace ... with your Fiddler trace output (or similar) from the actual call of your CosmosDB. I should have provided 2 copies of my actual payload as the last 2 responses. It is obvious as it is and is subject to change, but It worked for me . I hope this helps you and look forward to a modern library designed to interact with SOLID principles.