简体   繁体   English

DynamicData-无法将静态方法移动到另一个类(即使是基类)

[英]DynamicData - Unable to move the static method to another class (Even Base class)

I'm developing a Dynamic Data Test (c#) as described in https://www.meziantou.net/mstest-v2-data-tests.htm#using-dynamicdata . 我正在按照https://www.meziantou.net/mstest-v2-data-tests.htm#using-dynamicdata中的说明开发动态数据测试(c#)。

By keeping both the Dynamic Data Test and the static method within the same class, then all works fine, but when trying to move the static class to another class (Even Base class), then the test doesn't run and I get an error message: 通过将动态数据测试和静态方法都放在同一个类中,则可以正常工作,但是当尝试将静态类移动到另一个类(甚至是基本类)时,该测试无法运行,并且我得到一个错误信息:

Message: Value cannot be null. 消息:值不能为空。 Parameter name: Method GetData 参数名称:方​​法GetData

Can you please assist? 你能帮忙吗?

When moving the method to another class, I tried to make it as none-static too, but that didn't help. 将方法移到另一个类时,我也尝试使其变为非静态,但这没有帮助。

[TestClass]
public class MathTests
{
    [DataTestMethod]
    [DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
    public void Test_Add_DynamicData_Method(int a, int b, int expected)
    {
        var actual = MathHelper.Add(a, b);
        Assert.AreEqual(expected, actual);
    }

    public static IEnumerable<object[]> GetData()
    {
        yield return new object[] { 1, 1, 2 };
        yield return new object[] { 12, 30, 42 };
        yield return new object[] { 14, 1, 15 };
    }
}

Use the alternate constructor for the attribute that includes the type that contains the target data source 将替代构造函数用于包含包含目标数据源的类型的属性

For example 例如

[TestClass]
public class MathTests
{
    [DataTestMethod]
    [DynamicData(nameof(ExternalClass.GetData), typeof(ExternalClass), DynamicDataSourceType.Method)]
    public void Test_Add_DynamicData_Method(int a, int b, int expected)
    {
        var actual = MathHelper.Add(a, b);
        Assert.AreEqual(expected, actual);
    }

}

public class ExternalClass
{
    public static IEnumerable<object[]> GetData()
    {
        yield return new object[] { 1, 1, 2 };
        yield return new object[] { 12, 30, 42 };
        yield return new object[] { 14, 1, 15 };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM