Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate ResourceResponse<Document>

I am trying to mock a call that returns ResourceResponse<Document>, but I am not able to instantiate that type. Is there a factory class that can instantiate it or some other way to do so?

EDIT

var response = new ResourceResponse<Document>();

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

like image 836
Jonas Stawski Avatar asked Oct 21 '15 14:10

Jonas Stawski


2 Answers

The latest stable version of Microsoft.Azure.DocumentDB (1.10.0) atm added 2 constructors for mocking purposes.

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

Edit

Using Moq you could 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>()));

This way I can check if the method on my documentClient is being called, if you want to influence what is returned in the document, you have to create a document, and following that a ResourceResponse of that document. Something like:

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)));
like image 187
YentheO Avatar answered Nov 06 '22 17:11

YentheO


The ResourceResponse class has the constructor that contains the DocumentServiceResponse parameter marked as internal.

This is bad because even though you can create a ResourceReponse object from your DTO class you cannot set things like RUs consumed, response code and pretty much anything else because they are all coming from the ResourceResponseBase which also has the DocumentServiceResponse marked as internal.

Please find code at below link

how-to-mock-or-instantiate

like image 40
Magical Creations Avatar answered Nov 06 '22 18:11

Magical Creations