c# – 通用类/方法的单元测试方法

前端之家收集整理的这篇文章主要介绍了c# – 通用类/方法的单元测试方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
什么是推荐的方法来掩盖通用类/方法的单元测试?

例如(参考下面我的示例代码).是否有两到三倍的测试来覆盖使用几种不同类型的TKey,TNode类来测试方法?还是只有一堂课?

  1. public class TopologyBase<TKey,TNode,TRelationship>
  2. where TNode : NodeBase<TKey>,new()
  3. where TRelationship : RelationshipBase<TKey>,new()
  4.  
  5. {
  6. // Properties
  7. public Dictionary<TKey,NodeBase<TKey>> Nodes { get; private set; }
  8. public List<RelationshipBase<TKey>> Relationships { get; private set; }
  9.  
  10. // Constructors
  11. protected TopologyBase()
  12. {
  13. Nodes = new Dictionary<TKey,NodeBase<TKey>>();
  14. Relationships = new List<RelationshipBase<TKey>>();
  15. }
  16.  
  17. // Methods
  18. public TNode CreateNode(TKey key)
  19. {
  20. var node = new TNode {Key = key};
  21. Nodes.Add(node.Key,node);
  22. return node;
  23. }
  24.  
  25. public void CreateRelationship(NodeBase<TKey> parent,NodeBase<TKey> child) {
  26. .
  27. .
  28. .

解决方法

我通常创建一个DummyClass作为测试目的,作为通用参数传递(在你的情况下你应该创建3类),我测试一个类(TopologyBase)一次.

使用不同的通用类型进行测试是没有意义的,因为通用类型不应该破坏ToopologyBase类.

猜你在找的C#相关文章