【问题标题】:How to use REGEX in StepArgumentTransformation to find prefixed string如何在 StepArgumentTransformation 中使用 REGEX 查找前缀字符串
【发布时间】:2019-11-26 09:11:35
【问题描述】:

我想使用 SpecFlow 的 StepArgumentTransformation 将所有以“key:”为前缀的字符串转换为其他字符串。我不知道如何使用正确的正则表达式。

假设我的功能文件中有以下步骤:

Given a user is logged in with key:testkey and has password key:testpassword and username John

这导致了这个步骤定义:

[Given(@"a user is logged in with (.*) and has password (.*) and username (.*)")]
public void GivenUserIsLoggedIn(string key, string password, string username)
{
    // To stuff
}

现在我想通过使用 step 参数转换来更改键值:

[StepArgumentTransformation(@"add correct regular expression here")]
public string TransformKeys(string value)
{
    // Do transformation
    return result;
}

我只希望以“key:”为前缀的值进入转换方法。我怎样才能做到这一点?我尝试了几种类似

的东西
[StepArgumentTransformation(@"key:.*")
[StepArgumentTransformation(@"(key:.*)")
[StepArgumentTransformation(@"key:\w+")

但是我要么得到错误“参数计数不匹配”,要么根本没有输入转换方法。

【问题讨论】:

  • 尝试在参数周围添加单引号 (')。这使得在正则表达式中更容易确定参数。

标签: c# .net testing specflow gherkin


【解决方案1】:

注意:我使用 specflow3 作为版本

Scenario: stackoverflow usecase
    Given a user is logged in with key:testkey and has password key:testpassword and username John

在步骤定义中:

[Given(@"a user is logged in with (key:.*) and has password (key:.*) and username John")]
        public void GivenAUserIsLoggedInWithKeyTestkeyAndHasPasswordKeyTestpasswordAndUsernameJohn(string username, string password)
        {
            var user = username;
            var pass = password;

            Console.Write("username: {0} and password: {1} ", user, pass );
        }


[StepArgumentTransformation(@"key:(.*)")]
        public string ValueForKey(string value)
        {
            return value;
        }

如果为您工作,请接受答案。在您的情况下,specflow 版本是什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    相关资源
    最近更新 更多