Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock StoredProcedureResponse

The question is pretty simple. I've Moq'ed an IDocumentClient (which is the main DocumentDB .NET client). I am Moq'ing the ExecuteStoredProcedureAsync() method. It returns a StoredProcedureResponse type (a concrete type), but its interface is very locked down.

I figured I could just create a StoredProcedureResponse and embed my payload into the Response property, but it's setter is private. Moreover, the only constructor is parameterless.

What am I missing here? It would be ideal if the method returned an interface type (IStoredProcedureResponse), but I don't have control over that. I realize I could write a wrapper around the IDocumentClient, but that's not feasible.

The only thing I can think of is to extend and forcefully override the property with the "new" keyword - BUT, in the actual calling code, I would have a terrible hack in which I check the runtime type and downcast in order to use the override Resource property.

like image 278
Jmoney38 Avatar asked Nov 14 '25 21:11

Jmoney38


2 Answers

Is the IDocumentClient interface required at your top level? If not, you could create a service interface:

public interface IDocumentService
{
    Task<IStoredProcedureResponse<T>> ExecuteStoredProcedureAsync<T>(string query, IEnumerable<object> parameters);
}

In this case, or one similar, you could then implement a live service that uses DocumentClient and a mock service that just returns a mocked IStoredProcedureResponse.

Incidentally, I find it odd that IDocumentClient.ExecuteStoredProcedureAsync returns a concrete instance that ALSO happens to inherit from an interface.

like image 128
Eric Avatar answered Nov 17 '25 10:11

Eric


By reading the comments I found a simple solution:

var response = new StoredProcedureResponse<T>();
response.GetType().InvokeMember("responseBody",
                BindingFlags.Instance | BindingFlags.SetField | BindingFlags.NonPublic, Type.DefaultBinder, response, new object[] {new T()});
like image 43
Bagroy Avatar answered Nov 17 '25 11:11

Bagroy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!