如何在StepArgumentTransformation中使用REGEX查找前缀字符串

时间:2019-07-17 13:48:30

标签: c# .net testing specflow gherkin

我想使用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
}

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

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

我只希望前缀为“ key:”的值进入转换方法。我该如何实现?我尝试了

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

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

1 个答案:

答案 0 :(得分:0)

注意:我正在使用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版本是什么?