代码之家  ›  专栏  ›  技术社区  ›  Bryan Anderson

什么是GenericParameterHelper以及如何使用它?

  •  12
  • Bryan Anderson  · 技术社区  · 15 年前

    我在VS2008中对一个泛型类生成了单元测试,它使用了 GenericParameterHelper 在所有使用泛型类型的地方。这是一个应该被替换的占位符还是有一些用途?如果有的话,有什么用?

    以下是它作为示例生成的测试之一:

    /// <summary>
    ///A test for Count
    ///</summary>
    public void CountTestHelper<TKey, TValue>()
    {
        ObservableDictionary<TKey, TValue> target = new ObservableDictionary<TKey, TValue>(); // TODO: Initialize to an appropriate value
        int actual;
        actual = target.Count;
        Assert.Inconclusive("Verify the correctness of this test method.");
    }
    
    [TestMethod()]
    public void CountTest()
    {
        CountTestHelper<GenericParameterHelper, GenericParameterHelper>();
    }
    
    2 回复  |  直到 15 年前
        1
  •  8
  •   Craig Stuntz    15 年前

    假设你有一门课:

    public class Foo<T>
    {
        public bool DoSomething()
        {
            return false;
        }
    
        public T DoSomethingElse()
        {
        // ...
    }
    

    var foo = new Foo<T>();
    

    你必须使用真正的类型。但该方法中没有使用T,因此在测试中是噪声。因此,您可以:

    var foo = new Foo<GenericParameterHelper>();
    

        2
  •  0
  •   NER1808    10 年前

    正如你所说,它似乎只是一个占位符。我发现这是一个有用的页面,其中有一些示例,说明了如何编辑测试方法,使它们变得有用。

    http://msdn.microsoft.com/en-us/library/ms243401.aspx