gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:22:35 26 4
gpt4 key购买 nike

有没有人解决了如何结合 SpecFlow.Assist CreateInstance/CreateSet 将 SpecFlow Step Argument Transformations 应用于表格中的单元格的谜题? (此处合并代码以节省空间)

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绑定(bind)显然不适用于表单元格内容(因为该步骤的参数是表类型),但 SpecFlow.Assist CreateInstance/CreateSet 仍会以某种方式转换基本类型的单元格数据。

例如,如果生效日期的内容是“2016 年 11 月 13 日”而不是“2 天内”,则底层 poco 的 EffectiveDate 属性会很好地转换为 DateTime(或 int、decimal 等)。

我看到一些其他解决方案,例如在步骤定义本身内应用转换,如 here或创建一个 StepArgumentTransformation for the whole table ,但是......明显的缺点。更新:this question类似,但解决方案也避免将 StepArgumentTransformation 与 CreateInstance/CreateSet 混合。

SpecFlow Assist Helpers 中也有一个部分有关通过注册值检索器/比较器进行扩展的文档,但在我的示例中,日期时间集已经存在。那么,也许是自定义 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 应用于表格单元格内容我缺少什么的任何想法?或者所提到的解决方案之一是最好的/唯一的方法吗?

最佳答案

我创建了一个小型原型(prototype),可用于重新配置 Assist,以便能够使用 [StepArgumentTransformation] 绑定(bind)获取转换。

我的计划是写一篇关于它的博文,但在它准备好之前,也许你可以从这个要点中提取出本质。 (我一年前为 SpecFlow v2.0 做过,所以可能需要做一些较小的调整。)

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

关于c# - 使用 CreateInstance 对表格单元格内容进行 Specflow 步骤参数转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40553847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com