单元测试异常处理的内部构造第三方例外 [英] Unit testing exception handling for third party exceptions with internal constructors
问题描述
我们使用第三方的库与服务器软件,这是我们不能修改进行沟通。调用库函数可能会失败,抛出类似以下情况除外:
We use a third party library to communicate with their server software, which we can’t modify. Calls to the library can fail, by throwing exceptions similar to the following:
class LibraryException : Exception {
internal LibraryException() {};
string ExceptionDetails {public get; internal set; }
}
根据的ExceptionDetails,内容我们调用code具有执行不同的动作(有些错误是致命的,有些则没有),我想对此进行测试code。我的问题是,由于LibraryException有一个内部的构造,我无法找到创建一个方式。我试过new'ing一上来,派生子类,并使用Activator.CreateInstance。有什么办法解决这个工作?
Depending on the contents of ‘ExceptionDetails’, our calling code has to perform different actions (some errors are fatal, others are not), and I would like to test this code. The problem I have is that because ‘LibraryException’ has an internal constructor, I can’t find a way of creating one. I’ve tried new’ing one up, deriving a child class and using Activator.CreateInstance. Is there any way of working around this?
我想过修改构建,因此对于测试,将在一个不同的二进制链接的一个子集,但是这似乎是这将是一个维护的噩梦,所以此刻的区域仅覆盖集成测试
I’ve thought about changing the build, so that for a subset of tests it would link in a different binary, but this seems like it would be a maintenance nightmare, so at the moment the area is only covered by integration tests.
有什么建议?
推荐答案
它应该与 Activator.CreateInstance
使用接受过载时,的BindingFlags
:
it should work with Activator.CreateInstance
when using the overload that accepts BindingFlags
:
Activator.CreateInstance(typeof(LibraryException),
BindingFlags.NonPublic | BindingFlags.Instance,
null,
null,
null);
这篇关于单元测试异常处理的内部构造第三方例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!