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



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

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,我不明白为什么?该表填充良好,但我无法将其分配给变量"things",因为它只返回空

然后,当涉及到foreach循环时,我得到一个"System.NullReferenceException:"对象引用未设置为对象实例。">

同样,由于您需要一个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-或多行文本参数

最新更新