Unit test approach for generic classes/methods

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-05-12T11:02:22Z Indexed on 2010/05/12 11:14 UTC
Read the original article Hit count: 198

Filed under:
|
|
|

Hi,

What's the recommended way to cover off unit testing of generic classes/methods?

For example (referring to my example code below). Would it be a case of have 2 or 3 times the tests to cover testing the methods with a few different types of TKey, TNode classes? Or is just one class enough?

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) {
    .
    .
    .

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET