2015-10-02 31 views
6

Come posso inserire un valore nullo in Specflow attraverso una tabella? sguardoCome inserire un valore nullo nella tabella di definizione del passo specflow

Let ad un semplice esempio:

When a tire is attached to a car 
| CarId | TireModel  | FabricationDate | Batch | 
| 1  | Nokian Hakka R | 2015-09-1  |  | 

La stringa vuota nella colonna Batch viene interpretato come testo da specflow e, come tale, stringa vuota. Esiste una sintassi speciale per contrassegnare tale colonna come null?

risposta

7

È possibile creare il proprio IValueRetriever e sostituire uno predefinito con il tuo

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; 
    } 
} 

Alcuni dove nel tuo scenario passi

[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()); 
     } 
2

Non credo ci sia una sintassi speciale per null e penso che dovrai gestire la conversione da solo. Lo value retrievers have been revised in the v2 branch e si potrebbe essere in grado di gestirlo annullando la registrazione del valore standard del retriever e registrando la propria implementazione che cerca una sintassi speciale e restituisce null.

Nella versione corrente di 1.9. *, Tuttavia penso che dovrete solo controllare la stringa vuota e restituire nulla da soli.

1

Ho appena scelto di farlo caso per caso utilizzando un metodo di estensione semplice.

Nel gestore converto il passato nel parametro valore di esempio e chiamare NullIfEmpty()

Esempio di utilizzo

AndICheckTheBatchNumber(string batch) { 
    batch = batch.NullIfEmpty(); 
    //use batch as null how you intended 
} 

metodo di estensione

using System; 

namespace Util.Extensions 
{ 
    public static class StringExtensions 
    {   
     public static string NullIfEmpty(this string str) 
     { 
      if (string.IsNullOrEmpty(str)) 
      { 
       return null; 
      } 
      return str; 
     } 
    } 
} 
+1

Pensando a questo ulteriore, se questo è il test relativi SVS quindi non credo che gli utenti possono generalmente utilizzare Null nel mondo reale. – kernowcode