考虑以下代码:
private readonly Dictionary<Type, Component> _components = new Dictionary<Type, Component>();
public Component this [Type type]
{
get
{
Component component;
_components.TryGetValue(type, out component);
return component;
}
}
public void AddComponent(Component component)
{
_components.Add(component.GetType(), component);
}
正如您所看到的,
AddComponent
添加到私有变量 _components
。但是测试这一点的唯一方法是使用索引器。这很好,但为了测试索引器,我也必须调用 AddComponent
!换句话说,在索引器和
AddComponent
的单元测试中,每个测试都必须调用这两种方法。这似乎创建了不必要的耦合。如果索引器中存在错误,则没有理由使我的 TestAddComponent
失败。这里有什么最佳实践?我应该使用反射来获取
_components
吗?模拟?还是其他什么东西?