微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用CreateInstance对表格单元格内容进行Specflow步骤参数转换

有没有人解决过如何将SpecFlow Step Argument Transformations应用于表格中的单元格,以及SpecFlow.Assist CreateInstance / CreateSet? (代码组合起来节省空间)

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应用于表格单元格内容?或者是最好/唯一的解决方案之一?

解决方法

我创建了一个小型原型,可以用来重新配置Assist,以便能够通过[StepArgumentTransformation]绑定获取转换.

我的计划是制作一篇关于它的博客文章,但在它准备好之前,也许你可以从这个要点中脱颖而出. (我在一年前为SpecFlow v2.0做过,所以可能需要一些较小的适应性.)

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐