【发布时间】:2019-01-23 14:56:31
【问题描述】:
我正在测试一个类,该类有一个私有方法,并在内部从公共方法调用。我希望能够伪造这个测试方法,以便永远不会调用实际方法。
public class Service
{
public int MethodA(int a)
{
SaveToDB(a);
if (validate())
{
return a * 5;
}
else
{
return 0;
}
}
private bool validate(int a)
{
if (a > 100)
return true;
else
return false;
}
private bool SaveToDB()
{
// some logic to save to the database..
return true;
}
}
[FixtureTest]
public ServiceTest
{
//assuming we are using nunit and fakeiteasy..
[Test]
public void MethodA_Should_Return_Zero_when_Provided_100()
{
var fakeService = new Service;
var result = fakeservice.MethodA(101);
// I want to avoid the call SaveToDB() in the test how do I go about doing this..
//if this was a public method I could create a test stub and test like with a statement like
A.call(() => ServiceA.SaveToDB().Return())
// however this is a private function what should I do???
}
}
【问题讨论】:
-
这里有2个问题。 1)你不能访问类范围之外的私有方法。所以没有办法测试/模拟私有方法。 2)您不能模拟 db 调用,因为它是服务层的一部分。如果您将服务层和 DAL 层分开,这将更容易模拟。
-
我的第一个想法是依赖注入。如果服务会被注入一些 IDbSaver,它执行保存到数据库,你的测试很容易注入一个假的。
-
你可以反过来做。测试私有函数(通过反射)以证明它们有效,然后测试公共函数。不好用,用 DI 会更干净更好,但有时脏是唯一的办法(只要确保在关闭计算机时擦手即可)。
标签: c# unit-testing nunit fakeiteasy