有没有人解决了如何结合 SpecFlow.Assist CreateInstance/CreateSet 将 SpecFlow 步骤参数转换应用于表格中的单元格的难题? (这里合并代码以节省空间)
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);
}
}
步骤参数转换绑定显然不适用于表格单元格内容(因为该步骤的参数是 Table 类型),但以某种方式 SpecFlow.Assist CreateInstance/CreateSet 仍会将单元格数据转换为基本类型。
例如,如果有效日期的内容是“11/13/2016”而不是“2 天后”,则底层 poco 的有效日期属性会很好地转换为 DateTime(或 int、decimal 等)。
我看到了一些其他解决方案,例如在步骤定义本身中应用转换,例如here或创建 StepArgumentTransformation对于整个表,但是...明显的缺点。更新:这个问题类似,但解决方案还避免将 StepArgumentTransformation 与 CreateInstance/CreateSet 混合在一起。
中还有一个部分SpecFlow 辅助助手有关通过注册值检索器/比较器进行扩展的文档,但在我的示例中,日期时间集已经存在。那么,也许是自定义 DateTime 类型?看起来也许可以检查已知类型的 StepArgumentTransformations 或类似的东西。
In the 日期时间检索器, 就像是..
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 应用于表格单元格内容的任何想法?或者提到的解决方案之一是最好/唯一的方法吗?