【问题标题】:StepArgumentTransformation is not getting hitStepArgumentTransformation 没有被击中
【发布时间】:2020-11-13 22:24:59
【问题描述】:

我有一个场景大纲,其中包含向 oData Web API 发出 GET 请求以从中获取一些数据的场景。设想 验证从 API 返回的数据是否根据过滤器和正确的顺序。 Order by 子句是根据场景中提供的表构建的

Scenario Outline: Validate that data from API call for a given user is according to filters provided
    
Given for the user id of 101
Given default filters for GET request
Given the result multicolumn order direction is <firstColumn> <firstOrderby> then <secondColumn> <secondOrderby>
And following is unordered list of securities
    
| securityID | attribute1 | attribute2 | attribute3 | attribute4 |
| 16654      | active     | 0          | pending    | 33         |
| 16655      | active     | 0          | pending    | 33         |
| 16656      | active     | 0          | pending    | 33         |
| 16657      | active     | 0          | pending    | 33         |
| 16658      | inactive   | 4          | pending    | 33         |
| 16659      | active     | 0          | pending    | 33         |
| 16660      | active     | 0          | pending    | 33         |
| 16661      | active     | 0          | pending    | 33         |
| 16662      | active     | 0          | pending    | 33         |
| 16663      | inactive   | 0          | pending    | 33         |
| 16664      | inactive   | 2          | pending    | 33         |
    
When I invoke the API GET
Then the SecAPI should return HTTP <statusCode>
And the response securities should be in expected order in each <sampleName> with matching fields and record count of 11

Examples:
    | firstColumn | firstOrderby | secondColumn | secondOrderby | statusCode | sampleName |
    | securityID  | Asc          | attribute2   | Desc          | 200        | Asc-Desc   |
    | securityID  | Asc          | attribute2   | Asc           | 200        | Asc-Asc    |
    | securityID  | Desc         | attribute2   | Asc           | 200        | Desc-Asc   |
    | securityID  | Asc          | attribute2   | Desc          | 200        | Asc-Desc   |
    | securityID  | Asc          | attribute2   |               | 200        | Asc-Desc   |
    | securityID  |              | attribute2   |               | 200        | Asc-Desc   |

对于上述场景大纲,除了以下给出的声明外,一切正常:

Given the result multicolumn order direction is <firstColumn> <firstOrderby> then <secondColumn> <secondOrderby>

对于上述声明,我在steps.cs文件中有以下步骤

[Given(@"the result multicolumn (order direction is (.*) (.*) then (.*) (.*))")]
public void GivenTheResultOrderDirectionIs(StringWrapper orderBy)
{
    //step code here
}

并按照 steptransformation 将给定语句中的 4 个参数转换为正确的 oData orderBy 子句:

[Binding]
public class CustomTransforms
{
    [StepArgumentTransformation(@"order direction is <(\w+)> <(\w+)> then <(\w+)> <(\w+)>")]
    public StringWrapper OrderByTransform(string column1, string column1Direction, string column2, string column2Direction)
    {
       string orderByClause;
       
       //build clause here

        return new StringWrapper(orderByClause);
    }

}

问题是 OrderByClauseTransform 永远不会被调用。我遇到了以下异常:

抛出异常:TechTalk.SpecFlow.dll 中的“TechTalk.SpecFlow.BindingException” TechTalk.SpecFlow.dll 中出现“TechTalk.SpecFlow.BindingException”类型的异常,但未在用户代码中处理 参数计数不匹配!绑定方法'.......GivenTheResultMulticolumnOrderDirectionIs(StringWrapper)'应该有5个参数

【问题讨论】:

  • \w 不匹配 &lt; 也不匹配 &gt;。你需要two column order is &lt;(\w+)&gt; &lt;(\w+)&gt; then &lt;(\w+)&gt; &lt;(\w+)&gt;
  • 还是不行。错误与 Given... 方法需要 5 个参数相同。它不是在研究变换方法。如果我从给定...方法中删除额外的 () 则错误说它需要 4 个参数
  • 你能格式化你的场景大纲并更正它吗?存在一些格式错误,因此我们不确定这是否是导致问题的原因。
  • 我已经重新格式化并更新了场景大纲。我不允许发布真实代码,所以这是对问题的模拟。
  • 会不会是SpecFlow转换不支持多个参数?

标签: c# regex specflow


【解决方案1】:

步进转换只接收一个参数。这就是 SpecFlow 的工作原理。获得完全匹配的字符串后,使用 Regex 从该字符串中提取所需的片段。通过为 regex 模式声明一个常量,您可以在 Regex 对象以及 [StepArgumentTransformation] 属性中重用它:

[Binding]
public class CustomTransforms
{
    private const string OrderByPattern = @"order direction is (\w+) (\w+) then (\w+) (\w+)";

    private static readonly Regex OrderByRegex = new Regex(OrderByPattern);

    [StepArgumentTransformation(OrderByPattern)]
    public StringWrapper OrderByTransform(string text)
    {
        var match = OrderByRegex.Match(text);
        var column1 = match.Groups[1].Value;
        var direction1 = match.Groups[2].Value;
        var column2 = match.Groups[3].Value;
        var direction2 = match.Groups[4].Value;

        // Build your order by clause however you need to do it. For example, SQL:
        var orderByClause = $"ORDER BY {column1} {direction1}, {column2} {direction2}";

        return new StringWrapper(orderByClause);
    }
}

重要提示:步骤参数转换模式中的 &lt;&gt; 字符也会使事情变得混乱。在您的场景中,&lt;firstColumn&gt; 标记完全被示例表中的当前值替换。

当当前示例行是:

| firstColumn | firstOrderby | secondColumn | secondOrderby | statusCode | sampleName |
| securityID  | Asc          | attribute2   | Desc          | 200        | Asc-Desc   |

步骤:

Given the result multicolumn order direction is <firstColumn> <firstOrderby> then <secondColumn> <secondOrderby>

自动转换成这个:

Given the result multicolumn order direction is securityID Asc then attribute2 Desc

注意&lt;&gt; 字符在步骤的转换版本中不存在。尖括号用于表示在运行时由示例表中的数据替换的步骤的参数化部分。

【讨论】:

  • 感谢您提供此答案。我还没有测试它,因为我在处理更高优先级的问题,但答案看起来很棒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 2020-06-07
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多