Search code examples
c#unit-testingazureazure-cosmosdbmicrosoft-fakes

Creating ResourceResponse objects for unit tests with Azure DocDB


I am using the Microsoft Fakes unit testing framework for testing some methods that make queries to a DocumentDB database.

The DocumentClient class has several methods for making queries to DocDB (such as CreateDocumentAsync()) which return a ResourceResponse<Document> object wrapped up in a Task<T>.

I would like to shim CreateDocumentAsync() for unit testing purposes, however the return type, ResourceResponse<T>, doesn't appear to have a public constructor, despite mention of one in the documentation.

An extremely simplified version of what I want to accomplish is here:

[TestMethod]
public async Task MyTest() {
    using (ShimsContext.Create()) {
        // Arrange
        var docClient = new DocumentClient(new Uri("myUri"), "myKey");
         ShimDocumentClient.AllInstances.CreateDocumentAsyncUriObjectRequestOptionsBoolean =
            (DocumentClient instance, Uri uri, object document, RequestOptions options, bool disableAutomaticGeneration) =>
        {
            ResourceResponse<Document> response = new ResourceResponse<Document>(); // "error: does not contain a constructor that takes zero arguments"
            return response ;
        };

        // Act
        var response = await docClient.CreateDocumentAsync(new Uri("myCollectionUri"), "myDoc");

        // Assert
        Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
    }
}

How can I create a custom ResrouceResponse<Document> object to return in the shimmed method?


Solution

  • As noted in comment. v1.10 of the SDK supports ResourceResponse constructor will no arguments. The packages.config in the project solution should show the version of DocumentDB SDK the project is using: