如何在specflow步骤定义表中输入null值

Tuu*_*emi 10 bdd specflow gherkin

如何通过表在Specflow中输入空值?

让我们看一个过于简单的例子:

When a tire is attached to a car
| CarId | TireModel      | FabricationDate | Batch |
| 1     | Nokian Hakka R | 2015-09-1       |       |
Run Code Online (Sandbox Code Playgroud)

Batch列中的空字符串按specflow解释为文本,因此为空字符串.是否有特殊语法将该列标记为null?

Var*_*rma 16

从 SpecFlow 3 开始,在您的 Steps 类中,您只需放置以下代码即可。在功能文件中只需输入空值即可。现在,当您使用 CreateSet 函数时,它将被正确反序列化。

Id | Value
1  | <null> 

[Binding]
public static class YourStepClass
{
    [BeforeTestRun]
    public static void BeforeTestRun()
    {
        Service.Instance.ValueRetrievers.Register(new NullValueRetriever("<null>"));
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 12

您可以创建自己的IValueRetriever并用您的IValueRetriever替换默认值

public class StringValueRetriver : IValueRetriever
{
    public bool CanRetrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType)
    {
        return propertyType == typeof(string);
    }

    public object Retrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType)
    {
        return string.IsNullOrEmpty(keyValuePair.Value) ? null : keyValuePair.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

您的方案中的某些步骤

   [BeforeScenario]
    public void BeforeScenario()
    {
        var defaultStringValueRetriever = Service.Instance.ValueRetrievers.FirstOrDefault(vr => vr is TechTalk.SpecFlow.Assist.ValueRetrievers.StringValueRetriever);
        if (defaultStringValueRetriever != null)
        {
            Service.Instance.UnregisterValueRetriever(defaultStringValueRetriever);
            Service.Instance.RegisterValueRetriever(new StringValueRetriver());
        }
Run Code Online (Sandbox Code Playgroud)