SpecFlow:场景大纲示例

16

我刚开始使用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();
        }
    }
3个回答

17

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的全部威力。


7

已解决 - 使用字符,如@或&并不是问题所在。实际上,在我的Given语句中使用逗号才是问题所在。我发现如果使用'and'就可以了。所以为了让它正常工作,语句必须写成以下形式:

解决方案

  1. 将语句写成

    Given I have completed the form with <email> and <organisation> and <password> and <passwordConfirmation>

  2. 修改语句,将需要成为字符串的参数括在单引号中

    Given I have completed the form with '<email>' and '<organisation>' and '<password>' and '<passwordConfirmation>'

  3. 生成步骤定义,然后将语句改回不包含单引号

    Given I have completed the form with <email> and <organisation> and <password> and <passwordConfirmation>

有点折腾,但可以得到正确的结果。希望在未来SpecFlow会更新,以默认处理参数为字符串。


4
实际上,SpecFlow文档表示: 在占位符(例如'<placeholder>')周围放置单引号(')不是Gherkin语言所必需的,但有助于提高SpecFlow解析场景大纲的能力。 第三步在这里已经过时,不需要去掉引号也可以正常工作。 - qbik
@qbik非常感谢你的评论!实际上,Specflow文档的新版本并没有提到在占位符周围使用'。但是这种方法并不起作用。添加'解决了我的问题。 - undefined

0

供日后参考,如果Cater的答案不能解决问题。我遇到了以下问题

Give I have a <typeOfDay> day
When Im asked how I am
Then I will say <feeling>

Example:
|typeOfDay|feeeling|
|good     |happy  |
|bad      |sad    |

你会注意到 Then 语句中的“feeling”无法找到相应的值,因为有一个拼写错误。这会导致 SpecFlow 抛出“输入字符串格式不正确”的错误。在我的情况下,这花费了我尴尬地长时间才发现。

还有其他要检查的东西 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接