【发布时间】:2019-12-19 22:26:32
【问题描述】:
假设我有来自第三方库的以下类:
public class ThirdPartyType { ... }
public class ThirdPartyFunction
{
public ThirdPartyType DoSomething() { ... }
}
实现细节并不重要,它们实际上超出了我对这个第三方库的控制范围。
假设我为ThirdPartyFunction写了一个适配器类:
public class Adapter
{
private readonly ThirdPartyFunction f;
public Adapter()
{
f = new ThirdPartyFunction();
}
public string DoSomething()
{
var result = f.DoSomething();
// Convert to a type that my clients can understand
return Convert(result);
}
private string Convert(ThirdPartyType value)
{
// Complex conversion from ThirdPartyType to string
// (how do I test this private method?)
...
}
}
如何测试我的Convert(ThirdPartyType) 实现是否正确?只有Adapter 类需要它,这就是它是私有方法的原因。
【问题讨论】:
-
验证DoSomething,如果正确完成应该验证Convert...您通常不验证私有方法,但如果将其标记为内部,则可以将其公开给测试库。
-
@RonBeyer 我同意验证 DoSomething 将负责转换。但是,第三方库已经过非常彻底的测试(假设有数千次测试),我不想再重复所有这些测试。 “内部”的想法是可行的。
标签: c# unit-testing nunit xunit