覆盖泛型类/方法单元测试的推荐方法是什么?
例如(引用我下面的示例代码)。是否会有 2 或 3 次测试来涵盖使用几种不同类型的 TKey、TNode 类测试方法?还是只上一节课就够了?
public class TopologyBase<TKey, TNode, TRelationship>
where TNode : NodeBase<TKey>, new()
where TRelationship : RelationshipBase<TKey>, new()
{
// Properties
public Dictionary<TKey, NodeBase<TKey>> Nodes { get; private set; }
public List<RelationshipBase<TKey>> Relationships { get; private set; }
// Constructors
protected TopologyBase()
{
Nodes = new Dictionary<TKey, NodeBase<TKey>>();
Relationships = new List<RelationshipBase<TKey>>();
}
// Methods
public TNode CreateNode(TKey key)
{
var node = new TNode {Key = key};
Nodes.Add(node.Key, node);
return node;
}
public void CreateRelationship(NodeBase<TKey> parent, NodeBase<TKey> child) {
.
.
.
最佳答案
我通常创建一个 DummyClass 用于测试目的以作为通用参数传递(在您的情况下您应该创建 3 个类)并且我测试该类 (TopologyBase) 一次。
使用不同的泛型进行测试没有意义,因为泛型不应该破坏 ToopologyBase 类。
关于c# - 通用类/方法的单元测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2818125/