gpt4 book ai didi

c# - SpecFlow:场景大纲示例

转载 作者:可可西里 更新时间:2023-11-01 08:20:52 26 4
gpt4 key购买 nike

我刚开始使用 SpecFlow,非常喜欢这个工具。但是,我遇到了一些与场景大纲中的示例数据输入相关的问题。

只是想知道我所面对的是正常的还是有技巧的。

我正在使用 C# Visual Studio 2013 并使用步骤定义的下划线样式编写 MVC 应用程序。我也尝试过正则表达式样式,但仍然遇到类似的问题。

所以问题是我提供用户名、密码等作为参数,并在示例中包含示例数据。似乎发生了以下情况:-

  1. 第一次生成场景时,我必须在参数周围加上“”,否则根本不会将其作为参数选取。但是,当从示例中传递数据时,我在传递的数据末尾得到一个“/”。当我返回到场景时,我删除了参数周围的“”。这有点令人沮丧,但如果这是处理它的最佳方式,我可以接受。只是想知道是否有人对此有任何建议。

  2. 下一个问题与数据本身有关。如果我的数据中有任何字符(例如 @ 或 & 等),它似乎会在该点拆分该数据并将其提供给下一个参数,以便我得到不正确的数据。

我在下面包含了我的代码 - 如果有人有任何建议或资源可以查看,我们将不胜感激。

特征文件 功能:账户注册 为了在我的组织中使用 Mojito 服务 作为访客用户 我想创建一个具有管理权限的帐户

Scenario Outline: Register with valid details
Given I am on the registration page
And I have completed the form with <email> <organisation> <password> and <passwordConfirmation>
When I have clicked on the register button
Then I will be logged in as <username>
And my account will be assigned the role of <role>

Examples:
| email | organisation | password | passwordConfirmation | username | role |
| usernamea | Bytes | password1 | password1 | usernamea | Admin |
| usernameb | Bytes | password2 | password2 | usernameb | Admin |
| usernamec | Bytes | password3 | password3 | usernamec | Admin |
| usernamed | Bytes | password4 | password4 | usernamed | Admin |
| usernamee | Bytes | password5 | password5 | usernamee | Admin |

Scenario Outline: Register with invalid details
Given I am on the registration page
And I have completed the form with <email> <organisation> <password> and <passwordConfirmation>
When I have clicked on the register button
Then I will get an error message

Examples:
| email | organisation | password | passwordConfirmation |
| Jonesa@mojito.com | Bytes | 1LTIuta&Sc | wrongpassword |
| Jonesb@mojito.com | Bytes | 1LTIuta&Sc | 1LTIuta&Sc |
| Jonesc@mojito.com | No Organisation | 1LTIuta&Sc | 1LTIuta&Sc |

步骤生成的文件

[Binding]
public class AccountRegistrationSteps
{
[Given]
public void Given_I_am_on_the_registration_page()
{
ScenarioContext.Current.Pending();
}

[Given]
public void Given_I_have_completed_the_form_with_usernamea_Bytes_password_P0_and_password_P1(int p0, int p1)
{
ScenarioContext.Current.Pending();
}

[Given]
public void Given_I_have_completed_the_form_with_Jonesa_mojito_com_Bytes_P0_LTIuta_Sc_and_wrongpassword(int p0)
{
ScenarioContext.Current.Pending();
}

[When]
public void When_I_have_clicked_on_the_register_button()
{
ScenarioContext.Current.Pending();
}

[Then]
public void Then_I_will_be_logged_in_as_usernamea()
{
ScenarioContext.Current.Pending();
}

[Then]
public void Then_my_account_will_be_assigned_the_role_of_Admin()
{
ScenarioContext.Current.Pending();
}

[Then]
public void Then_I_will_get_an_error_message()
{
ScenarioContext.Current.Pending();
}
}

最佳答案

SpecFlow 确实默认处理字符串参数,问题是您将控制权留给 SpecFlow 来决定在运行时您的值是什么。

当您运行“生成步骤定义”时,您在“样式”下拉列表中选择了“方法名称 - 下划线”。这将解释输入参数直到 SpecFlow,它将创建所谓的“贪婪”正则表达式来识别参数值。这意味着它将包含逗号作为值的一部分。

如果您选择了“属性中的正则表达式”(或重构您的代码并手动装饰您的属性),您的步骤可能如下所示:

[Given(@"I have completed the form with (.*), (.*), (.*), and (.*)")]
public void Given_I_have_completed_the_form_with(string email, string org, string pwd, string conf)
{
//do stuff here
}

这会创建一个更“简洁”的表达式,告诉 SpecFlow 接受任何长度的字符串,最多但不包括任何尾随逗号。正则表达式周围的单引号会使它更加明确:

[Given(@"I have completed the form with '(.*)', '(.*)', '(.*)', and '(.*)'")]

自己管理正则表达式可能会让人头疼,但如果您这样做,它确实会发挥 SpecFlow 的全部功能。

关于c# - SpecFlow:场景大纲示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25388438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com