简体   繁体   English

如何模拟 Azure DocumentDB 客户端库引发的 DocumentClientException?

[英]How do I mock the DocumentClientException that the Azure DocumentDB client library throws?

I'm trying to write some unit tests around code that queries Azure Document DB.我正在尝试围绕查询 Azure 文档 DB 的代码编写一些单元测试。 In particular, I'm trying to ensure that error handling works correctly.特别是,我试图确保错误处理正常工作。 The only difficulty is that I can't mock the DocumentClientException class that the client library throws when it receives an error from DocumentDB.唯一的困难是我无法模拟客户端库在从 DocumentDB 收到错误时抛出的DocumentClientException类。 DocumentClientException implements ISerializable , so when I try to mock it (with Moq), I get an exception saying that the mock object failed to provide a deserialization constructor. DocumentClientException实现了ISerializable ,所以当我尝试模拟它(使用 Moq)时,我得到一个异常,说模拟对象未能提供反序列化构造函数。

Has anyone successfully mocked the Azure DocumentDB document client exception?有没有人成功模拟过 Azure DocumentDB 文档客户端异常? If so, how did you do it?如果是这样,你是怎么做到的? Or is my testing strategy completely off?还是我的测试策略完全关闭?

Your unit test code should not connect to the real Azure DocumentDb.你的单元测试代码不应连接到真正的 Azure DocumentDb。

wrap your Azure client class in an interface, and in the concrete class' method, make the DocumentDB call.将 Azure 客户端类包装在一个接口中,并在具体类的方法中进行 DocumentDB 调用。 this is your real code.这是你真正的代码。

and when you are unit testing the caller class of this method, mock the wrapper interface, inject it into the class being tested and throw a DocumentClientException yourself in the Moq setup.并且当您对该方法的调用者类进行单元测试时,模拟包装器接口,将其注入正在测试的类中,并在 Moq 设置中自己抛出 DocumentClientException。 and test the scenario.并测试场景。

Since DocumentClientException only has internal constructors, it is worth having a test method to construct this using reflection.由于 DocumentClientException 只有内部构造函数,因此值得拥有一个使用反射构造它的测试方法。 (this is surely a perf-hit, but being test code, you could opt for testability vs. a few milliseconds) (这肯定是一个性能打击,但作为测试代码,您可以选择可测试性而不是几毫秒)

private static DocumentClientException CreateDocumentClientExceptionForTesting(
                                               Error error, HttpStatusCode httpStatusCode)
{
    var type = typeof (DocumentClientException);

    // we are using the overload with 3 parameters (error, responseheaders, statuscode)
    // use any one appropriate for you.

    var documentClientExceptionInstance = type.Assembly.CreateInstance(type.FullName, 
        false, BindingFlags.Instance | BindingFlags.NonPublic, null,
        new object[] {error, (HttpResponseHeaders) null, httpStatusCode}, null, null);

    return (DocumentClientException)documentClientExceptionInstance;
}

And then have callers use them as,然后让呼叫者将它们用作,

var error = new Error
{
    Id = Guid.NewGuid().ToString(),
    Code = "some_code",
    Message = "some_message"
};

var testException = CreateDocumentClientExceptionForTesting(error, HttpStatusCode.InternalServerError);

// make Moq throw testException

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

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