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

标签 c# .net unit-testing generics

覆盖泛型类/方法单元测试的推荐方法是什么?

例如(引用我下面的示例代码)。是否会有 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/

相关文章:

.net - 通过 MTP C#/VB.net 将文件写入 WPD 设备

c# - 如何检测流中的图像格式

unit-testing - 单元测试中的 Grails Spock 模拟注入(inject)服务

javascript - Angular js单元测试mock文档

c# - ASP.NET 核心 3.1。更新集合的问题

c# - 用代码模拟窗口拖放?

c# - MonoTouch - UIDevice.CurrentDevice.Name - UTF8

python - 具有基于文件的电子邮件后端服务器的 Django 测试框架

c# - 如何获取 HttpContext 调用的 Web 服务方法?

c# - 在深度异步调用中查找父方法的属性