简体   繁体   English

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

[英]How to use REGEX in StepArgumentTransformation to find prefixed string

I want to use StepArgumentTransformation of SpecFlow to transform all strings prefixed with "key:" to something else. 我想使用SpecFlow的StepArgumentTransformation将所有以“ key:”为前缀的字符串转换为其他字符串。 I don't get around on how to use the right regular expression. 我不了解如何使用正确的正则表达式。

Lets say I have the following step in my feature file: 可以说我在功能文件中有以下步骤:

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

Which leads to this step definition: 这导致此步骤定义:

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

Now I want to change the key values by using step argument transformation: 现在,我想通过使用逐步参数转换来更改键值:

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

I only want values prefixed with "key:" to enter the transformation method. 我只希望前缀为“ key:”的值输入转换方法。 How can I achieve this? 我该如何实现? I tried several stuff like 我尝试了一些类似的东西

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

But i either get error "parameter count mismatch" or the transformation method isn't entered at all. 但我要么收到错误“参数计数不匹配”,要么根本没有输入转换方法。

Note: I am using specflow3 as version 注意:我正在使用specflow3作为版本

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

In step definition: 在步骤定义中:

[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;
        }

Please accept the answer if working for you. 如果为您工作,请接受答案。 What is the specflow version in your case? 您使用的Specflow版本是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM