简体   繁体   English

如何模拟StoredProcedureResponse

[英]How to mock StoredProcedureResponse

The question is pretty simple. 问题很简单。 I've Moq'ed an IDocumentClient (which is the main DocumentDB .NET client). 我已经使用了一个IDocumentClient(这是主要的DocumentDB .NET客户端)。 I am Moq'ing the ExecuteStoredProcedureAsync () method. 我是Moq'ing ExecuteStoredProcedureAsync ()方法。 It returns a StoredProcedureResponse type (a concrete type), but its interface is very locked down. 它返回StoredProcedureResponse类型(具体类型),但其界面非常锁定。

I figured I could just create a StoredProcedureResponse and embed my payload into the Response property, but it's setter is private. 我想我可以创建一个StoredProcedureResponse并将我的有效负载嵌入到Response属性中,但它的setter是私有的。 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. 如果方法返回一个接口类型(IStoredProcedureResponse),那将是理想的,但我无法控制它。 I realize I could write a wrapper around the IDocumentClient, but that's not feasible. 我意识到我可以在IDocumentClient周围编写一个包装器,但那是不可行的。

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. 我唯一能想到的是使用“new”关键字扩展并强制覆盖属性 - 但是,在实际的调用代码中,我会有一个可怕的黑客,我检查运行时类型并向下转换以便使用覆盖资源属性。

Is the IDocumentClient interface required at your top level? 您的顶级是否需要IDocumentClient接口? 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. 在这种情况下,或者类似的,您可以实现一个使用DocumentClient的实时服务和一个只返回模拟的IStoredProcedureResponse的模拟服务。

Incidentally, I find it odd that IDocumentClient.ExecuteStoredProcedureAsync returns a concrete instance that ALSO happens to inherit from an interface. 顺便说一句,我发现IDocumentClient.ExecuteStoredProcedureAsync返回一个也恰好从接口继承的具体实例。

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()});

Use reflection to create it: 使用反射来创建它:

Type type=typeof(StoredProcedureResponse);
var spr = (StoredProcedureResponse)Activator.CreateInstance(type,true);

and set the properties and whatever else you need using reflection as well: 并使用反射设置属性和其他任何你需要的东西:

spr.GetType().InvokeMember("PropertyName",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder, obj, "MyName");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM