2016-11-11 18 views
4

是否有人解決了如何將SpecFlow步驟參數轉換應用於表格中的單元格以及SpecFlow.Assist CreateInstance/CreateSet的謎題? (這裏結合的編碼,以節省空間)使用CreateInstance指定表格單元格內容的步驟參數轉換

Given a table like the following: 
| Price | Zip | Effective Date | 
| 10.00 | 90210 | in 2 days  | 
When the 'given' step executes 
And the table data populates a poco 
Then the effective date should be transformed into a DateTime with value of 2 days from today 

[Given(@"a table like the following:")] 
public void GivenATableLikeTheFollowing(Table table) 
{ 
    var temp = table.CreateInstance<Temp>(); 
} 

internal class Temp 
{ 
    decimal Price { get; set; } 
    int Zip { get; set; } 
    DateTime EffectiveDate { get; set; } 
} 

[Binding] 
public class Transforms 
{ 
    [StepArgumentTransformation(@"in (\d+) days?")] 
    public DateTime InXDaysTransform(int days) 
    { 
     return DateTime.Today.AddDays(days); 
    } 
} 

StepArgumentTransformation綁定顯然並不適用於表格單元格的內容(因爲步驟的說法是類型表),但不知何故SpecFlow.Assist的CreateInstance/CreateSet仍將變換單元格數據對於基本類型。

例如,如果生效日期的內容是'11/13/2016'而不是'2天內',則底層poco的EffectiveDate屬性會轉換爲DateTime(或int,decimal等)。

我看到一些其他解決方案,如在步驟定義中應用轉換本身,如here或創建StepArgumentTransformation for the whole table,但...明顯的缺點。更新:this question是類似的,但解決方案也避免與CreateInstance/CreateSet混合StepArgumentTransformation。

SpecFlow Assist Helpers文檔中還有關於通過註冊值檢索器/比較器進行擴展的文檔,但在我的示例中,一個DateTime集已存在。那麼,也許是一個自定義的DateTime類型?似乎也許可能會檢查已知類型的StepArgumentTransformations,或類似的東西。

DateTime retriever,像..

public virtual DateTime GetValue(string value) 
    { 
     var returnValue = DateTime.MinValue; 
     // check for StepArgumentTransformations here first? 
     DateTime.TryParse(value, out returnValue); 
     return returnValue; 
    } 

什麼我缺少獲得使用table.CreateInstance時應用到表單元格內容的StepArgumentTransformation任何想法?或者是提到的解決方案之一是最好的/唯一的方法?

回答

1

我創建了一個小型原型,可用於重新配置Assist,以便能夠使用[StepArgumentTransformation]綁定獲取轉換。

我的計劃是做一個關於它的博客文章,但在準備好之前,也許你可以從這個要點中脫穎而出。 (我做了一年前爲2.0 SpecFlow,所以一些規模較小的adaptions可能是必要的。)

https://gist.github.com/gasparnagy/a478e5b7ccb8f557a6dc

+0

謝謝加斯帕爾,期待您的博客文章。您是否打算在將來添加此功能作爲功能,還是將其作爲自定義選項保留? –

0

我不認爲你想要的是當前實現的,但理論上我認爲它可以實現。您可能自己實現一個新的增強DateTimeValueRetriever,它自己檢查該字符串是否可以先解析爲日期時間,如果不檢查是否有任何[StepArgumentTransformation]方法可以解析它,然後再檢查replace the current DateTimeValueRetriever with your enhanced one。然後你可以提交你的新版本,作爲對現有版本的增強,看看胃口是什麼。

+0

似乎很多可以從中受益。可能會超出我目前的熟悉程度,但如果沒有其他問題,我們會進行調查。 –

相關問題