【问题标题】:xUnit - Passing List<> to my test is throwing an error [duplicate]xUnit - 将 List<> 传递给我的测试会引发错误 [重复]
【发布时间】:2022-01-15 13:23:47
【问题描述】:

我正在考虑使用 xUnit 进行测试。我喜欢它的一些功能。

我正在尝试创建一个列表并将该列表作为测试的参数传递:

    [Theory]
    [InlineData( new List<IDispenseEntity>() )]
    public void Test1(List<IDispenseEntity> data)
    {

        // Some test here Assert.Equal(2, 2);
    }

我得到:

属性参数必须是常量表达式,typeof 表达式 或属性参数类型的数组创建表达式

我想用列表中元素的不同组合来测试各种列表。

如何将 List 传递给我的测试?

【问题讨论】:

    标签: c# xunit


    【解决方案1】:

    InlineData 属性只接受编译时值(常量)。
    寻找MemberData

    [Theory]
    [MemberData(nameof(EmptyList))]
    public void Test1(List<IDispenseEntity> data)
    {
    
        // Some test here Assert.Equal(2, 2);
    }
    
    public static IEnumerable<object[]> EmptyList()
    {
        yield return new[] { new List<IDispenseEntity>() };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2017-08-24
      相关资源
      最近更新 更多