Unit testing 如何在静态函数中调用特定函数
我想测试一个特定的函数。 该函数调用了另一个类的静态方法,该类受保护,因此无法从外部访问。 当我在做组件级测试时,我不想触及数据库。 所以,如果对数据库的特定调用位于静态函数中,是否可以对其进行模拟Unit testing 如何在静态函数中调用特定函数,unit-testing,c#-4.0,delegates,moles,Unit Testing,C# 4.0,Delegates,Moles,我想测试一个特定的函数。 该函数调用了另一个类的静态方法,该类受保护,因此无法从外部访问。 当我在做组件级测试时,我不想触及数据库。 所以,如果对数据库的特定调用位于静态函数中,是否可以对其进行模拟 //I want to test this function public void testing { Abc.instance.Add(); } class Abc { public static readOnly instance = new Abc(); A
//I want to test this function
public void testing
{
Abc.instance.Add();
}
class Abc
{
public static readOnly instance = new Abc();
Abc()
{
createInstance();
}
public void createInstance()// I want to mock this function
{
//calls to the database
}
public void Add()
{
//...
}
}
但即使我使用delegate来模拟createInstance()
,甚至在进入委托行之前,静态块也会被调用,从而命中数据库并引发异常