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应用于表格单元格内容?或者是最好/唯一的解决方案之一?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。