如果这是重复的,我深表歉意。我被赋予了为该方法添加一些覆盖范围的任务,并被告知要模拟私有List<string>
属性。我的问题是:有没有办法测试私有字段?
我找到的解决方案是添加新的构造函数来注入这个私有列表。我不确定这是否是正确的方法,所以任何帮助将不胜感激。
public class Class1
{
public Class1(List<string> list)//This is just for Unit Testing
{
list1 = list;
}
private readonly InjectRepository _repository;
//
public Class1(InjectRepository repository)//This is the actual constructor
{
_repository = repository;
}
private List<string> list1 = new List<string>();
public void Do_Complex_Logic()
{
//list1 will be set with items in it
//Now list1 is passed to some other instance
}
}