是否有人解決了如何將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任何想法?或者是提到的解決方案之一是最好的/唯一的方法?
謝謝加斯帕爾,期待您的博客文章。您是否打算在將來添加此功能作爲功能,還是將其作爲自定義選項保留? –