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

Specflow step argument transformation on table cell contents with CreateInstance(使用 CreateInstance 对表格单元格内容进行 Specflow 步骤参数转换)
本文介绍了使用 CreateInstance 对表格单元格内容进行 Specflow 步骤参数转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Has anyone solved the riddle of how to apply SpecFlow Step Argument Transformations to cells in a table, in conjunction with the SpecFlow.Assist CreateInstance/CreateSet? (code combined here to save 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 类型),但 SpecFlow.Assist CreateInstance/CreateSet 仍会以某种方式将单元格数据转换为基本类型.

StepArgumentTransformation bindings apparently don't apply to table cell contents (since the step's argument is type Table), but somehow the SpecFlow.Assist CreateInstance/CreateSet will still transform cell data for basic types.

例如,如果有效日期的内容是2016 年 11 月 13 日"而不是2 天内",则基础 poco 的 EffectiveDate 属性可以转换为 DateTime(或 int、decimal 等).

For example , if the Effective Date's contents are '11/13/2016' instead of 'in 2 days', the underlying poco's EffectiveDate property transforms to a DateTime just fine (or an int, decimal, etc).

我看到了一些其他解决方案,例如在步骤定义本身中应用转换,例如 在此处 或创建 StepArgumentTransformation 整个表,但是......明显的缺点.更新:这个问题类似,但是解决方案还避免将 StepArgumentTransformation 与 CreateInstance/CreateSet 混合.

I see some other solutions like applying a conversion within the step definition itself like here or creating a StepArgumentTransformation for the whole table, but... obvious cons. Update: this question is similar, but solutions also avoid mingling StepArgumentTransformation with CreateInstance/CreateSet.

SpecFlow Assist Helpers 文档中还有一个部分关于通过注册值检索器/比较器进行扩展,但在我的示例中,日期时间集已经存在.那么,也许是自定义 DateTime 类型?似乎可能会检查已知类型的 StepArgumentTransformations 或类似的东西.

There is also a section in the SpecFlow Assist Helpers docs about extending by registering value retrievers/comparers, but in my example, a DateTime set already exists. So, perhaps a custom DateTime type? It seems like perhaps there could be a check for StepArgumentTransformations on the known types, or something like that.

在 DateTime 检索器中,类似..

    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 应用于表格单元格内容的任何想法?还是提到的解决方案之一是最好/唯一的方法?

Any ideas on what I am missing to get the StepArgumentTransformation to apply to the table cell contents when using table.CreateInstance? Or is one of the mentioned solutions the best/only way?

推荐答案

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

I have created a small prototype that can be used to reconfigure Assist to be able to pick up conversions with [StepArgumentTransformation] bindings.

我的计划是写一篇关于它的博客文章,但在它准备好之前,也许你可以从这个要点中得到精髓.(我在一年前为 SpecFlow v2.0 做过,因此可能需要进行一些较小的调整.)

My plan is to make a blog post about it, but until it is ready, maybe you can get out the essence from this gist. (I did it a year ago for SpecFlow v2.0, so some smaller adaptions might be necessary.)

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

这篇关于使用 CreateInstance 对表格单元格内容进行 Specflow 步骤参数转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)