【问题标题】:Using StepArgumentTransformation to transform *any* string argument into a different string使用stepargumentraLansformation将*任何*字符串参数转换为不同的字符串
【发布时间】:2014-02-26 05:26:12
【问题描述】:

对于我们的 SpecFlow 场景,我们需要在字符串参数到达 step 方法之前拦截它们,因为我们正在对这些值进行一些自定义处理。我正在尝试添加一个通用的 StepArgumentTransformation 属性,我假设它应该捕获任何字符串参数,但它永远不会被击中:

[Binding]
public class Transforms
{
    [StepArgumentTransformation]
    public string StringTransform(string value)
    {
        // Transform code is here...
    }
}

我唯一能想到的是不可能只做一个通用的字符串 -> 字符串转换?

有人有什么想法吗?谢谢!

【问题讨论】:

  • 知道为什么您不只指定作为转换结果的字符串会很有趣?
  • 我不确定你的意思?
  • 如果您知道字符串“X”将被转换为具有值“Y”,那么为什么不在场景中指定“Y”呢?如果您在问题中包含示例场景,我可能会理解您为什么要进行通用转换。

标签: c# binding automation bdd specflow


【解决方案1】:

当 SpecFlow 是一个字符串并且您无法截获它时,它会进行自动转换。 来自文档:

The following conversions can be performed by SpecFlow (in the following precedence):
 - no conversion, if the argument is an instance of the parameter type (e.g. the parameter type is object or string)
 - step argument transformation
 - standard conversion

一种解决方法是定义一个具有单个属性的自定义类型来保存自定义字符串值:

public class CustomString
{
    public string Value {get; set;}
}

现在您可以使用该属性将匹配某个正则表达式的字符串转换为该自定义类。

更多信息: http://www.specflow.org/documentation/Step-Argument-Conversions/

【讨论】:

  • 谢谢!由于 SpecFlow 的内置功能,我几乎认为没有办法执行 string -> string。我认为这是一个值得的解决方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 2019-10-10
  • 2016-03-23
  • 1970-01-01
  • 2019-01-05
  • 2011-06-18
相关资源
最近更新 更多