使用 CreateInstance 对 table 单元格内容进行 Specflow 步骤参数转换
Specflow step argument transformation on table cell contents with CreateInstance
有没有人解决了如何将 SpecFlow Step Argument Transformations 应用于 table 中的单元格以及 SpecFlow.Assist CreateInstance/CreateSet 的谜题? (此处代码合并保存space)
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 绑定显然不适用于 table 单元格内容(因为该步骤的参数类型为 Table),但不知何故 SpecFlow.Assist CreateInstance/CreateSet 仍将转换基本类型的单元格数据。
例如,如果有效日期的内容是“11/13/2016”而不是 'in 2 days',底层 poco 的 EffectiveDate 属性 转换为 DateTime 就好了(或 int,decimal , ETC)。
我看到一些其他解决方案,例如在步骤定义本身内应用转换,如 here or creating a StepArgumentTransformation for the whole table, but... obvious cons. Update: 类似,但解决方案也避免将 StepArgumentTransformation 与 CreateInstance/CreateSet 混合在一起。
SpecFlow Assist Helpers 文档中还有一个部分是关于通过注册值 retrievers/comparers 进行扩展的,但在我的示例中,日期时间集已经存在。那么,也许是自定义 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 应用于 table 单元格内容所缺少的任何想法?还是上述解决方案之一 best/only 方式?
我觉得你想要的目前没有实现,但是理论上我觉得是可以实现的。您可能可以自己实现一个新的、增强的 DateTimeValueRetriever
,它首先检查字符串是否可解析为日期时间,如果不能,则检查是否有任何 [StepArgumentTransformation]
方法可以解析它,然后 replace the current DateTimeValueRetriever
with your enhanced one.然后你可以提交一份 pr,提供你的新版本作为对现有版本的增强,看看有什么胃口。
我创建了一个小型原型,可用于重新配置 Assist 以便能够通过 [StepArgumentTransformation]
绑定获取转化。
我的计划是写一篇关于它的博客 post,但在它准备好之前,也许您可以从这个要点中提取精华。 (我一年前为 SpecFlow v2.0 做过,所以可能需要做一些较小的调整。)
有没有人解决了如何将 SpecFlow Step Argument Transformations 应用于 table 中的单元格以及 SpecFlow.Assist CreateInstance/CreateSet 的谜题? (此处代码合并保存space)
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 绑定显然不适用于 table 单元格内容(因为该步骤的参数类型为 Table),但不知何故 SpecFlow.Assist CreateInstance/CreateSet 仍将转换基本类型的单元格数据。
例如,如果有效日期的内容是“11/13/2016”而不是 'in 2 days',底层 poco 的 EffectiveDate 属性 转换为 DateTime 就好了(或 int,decimal , ETC)。
我看到一些其他解决方案,例如在步骤定义本身内应用转换,如 here or creating a StepArgumentTransformation for the whole table, but... obvious cons. Update:
SpecFlow Assist Helpers 文档中还有一个部分是关于通过注册值 retrievers/comparers 进行扩展的,但在我的示例中,日期时间集已经存在。那么,也许是自定义 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 应用于 table 单元格内容所缺少的任何想法?还是上述解决方案之一 best/only 方式?
我觉得你想要的目前没有实现,但是理论上我觉得是可以实现的。您可能可以自己实现一个新的、增强的 DateTimeValueRetriever
,它首先检查字符串是否可解析为日期时间,如果不能,则检查是否有任何 [StepArgumentTransformation]
方法可以解析它,然后 replace the current DateTimeValueRetriever
with your enhanced one.然后你可以提交一份 pr,提供你的新版本作为对现有版本的增强,看看有什么胃口。
我创建了一个小型原型,可用于重新配置 Assist 以便能够通过 [StepArgumentTransformation]
绑定获取转化。
我的计划是写一篇关于它的博客 post,但在它准备好之前,也许您可以从这个要点中提取精华。 (我一年前为 SpecFlow v2.0 做过,所以可能需要做一些较小的调整。)