SpecFlow - C# - var abc =table.CreateInstance<>() null

SpecFlow - C# - var abc =table.CreateInstance<>() null

我有这个场景。基本上我想以某些角色登录,然后在我的测试中枚举 table 个值,看看它们是否都可见。

Scenario Outline: View something with user.
    Given I navigate to the application with "<Role>"
    When I view something for "<SubRole>"
    Then all things should be viewable for "<SubRole>"
        | Sequence | Things |
        | 1        | thing1 |
        | 2        | thing2 |
        | 3        | thing3 |
        | 4        | thing4 |

    @Dev
    Examples:
        | Role       | SubRole|
        | System role| user1  |
 [Then(@"all things should be viewable for ""(.*)""")]
        public void ThenAllThingsShouldBeViewableFor(string subRole, Table table)
        {
         
             var things = table.CreateInstance<AllThings>();

   foreach (var stuff in things)
           {
             //code to check if all things in table are viewable
           }
public class AllThings
{
    public string Sequence { get; set; }
    public string Things{ get; set; }

}

但是对于 Sequence 和 Things 来说 var things = null,我不知道为什么? table 填充得很好,但我不能将它分配给变量 'things',因为它只是 returns null

然后当谈到 foreach 循环时,我得到一个 'System.NullReferenceException: 'Object reference not set to an instance of an object.''

此外,由于您希望使用 Enumerable,因此必须使用 CreateSet 而不是 CreateInstance

所以它应该是这样的:

[Then(@"all things should be viewable for ""(.*)""")]
public void ThenAllThingsShouldBeViewableFor(string subRole, Table table)
{
    var things = table.CreateSet<AllThings>();
    foreach (var stuff in things)
    {
          //code to check if all things in table are viewable
    }
}

查看文档:https://docs.specflow.org/projects/specflow/en/latest/Bindings/Step-Definitions.html#table-or-multi-line-text-arguments