c# - 如何在 StepArgumentTransformation 中使用 REGEX 查找带前缀的字符串

标签 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+")

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

最佳答案

注意:我使用的是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;
        }

关于c# - 如何在 StepArgumentTransformation 中使用 REGEX 查找带前缀的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57077468/

相关文章:

c# - NUnit:在单个测试中运行多个断言

c# - 如何查找 PDF 文档的条形码

c# - IEnumerable<T> 评估和单元测试

c# - c#.net 和 vb.net 中同一 dll 的属性差异

Django:在测试用例中获取cookie

c# - MySQL .Net 连接器连接已打开但已关闭

android - 使用适用于 Android 的 Xamarin.Forms 在 Android 模拟器中显示 MSAL 登录页面时出现问题

python - 为具有多对多关系的 Django 模型编写测试

unit-testing - 在测试抛出的异常时,我应该测试多少异常?

c# - C# 中的正则表达式电子邮件验证器中的问题