简体   繁体   English

使用Behat的最佳做法

[英]Best practices using Behat

I want to test a route for adding an event. 我想测试添加事件的路线。 But the problem is that : this method send a lot of arguments in POST about : 50. I have tried : 但是问题是:这个方法在POST中发送了很多关于50的参数。

Scenario Outline: Check Api Simple Test
Given  I use http method "POST"
And    I have param "sEventType" with value "<sEventType>"
And    I have param "aFilters[]" with value "<aFilters[]>"
And    I have param "nCompany" with value "<nCompany>"
..................................................
And    I call url "<path>"
And    I should to have "code" with value "<code>"
And    I should to have "error" with value "<error>"
Examples : 
|path ........
|..............

For a lot of many arguments this Examples will make the feature illegibly. 对于许多争论,本示例将使该功能难以理解。 What is the best practice to test this route with many arguments. 用许多参数测试此路线的最佳实践是什么。 Please help me and thanks in advance! 请帮助我,在此先感谢!

A feature test with a long list of steps is an anti-pattern. 具有大量步骤的功能测试是反模式。 Do more within each step, written in code, and then re-use those steps as appropriate. 在每个步骤中,以代码编写,然后执行更多操作,然后根据需要重新使用这些步骤。 I've got some feature contexts that just pull in a few Traits that can work together to do what I need. 我有一些功能上下文,它们可以引入一些特质,这些特质可以一起工作以完成我需要的工作。

Sometimes, the code can be a list of what would be the web-steps (I've got a register function that is visit('url') fillFields(), pressButton()), other times they read or write to the database. 有时,代码可以是网络步骤的列表(我有一个注册函数,它是visit('url')fillFields(),pressButton()),而其他时候它们是读取或写入数据库的。

Scenario Outline: Check Api Simple Test
Given  I prepare an API with appropriate parameters
When   I call url "<path>"
Then   I should to have "code" with value "<code>"
 And   I should to have "error" with value "<error>"

Behat is for acceptance testing, what you are trying is called an integration test. Behat是用于验收测试的,您正在尝试的称为集成测试。

If you want these post vars posted, just visit(the url), optionally fill in the form and then submit. 如果要发布这些帖子变量,只需访问(URL),可以选择填写表格,然后提交。 It is exactly how your users will have to fill the form. 这正是用户必须填写表格的方式。 If it's too much for you, maybe it's too much for you users. 如果对您来说太多了,也许对您的用户来说太多了。

When I fill in "form_element_name" with "value"
And I press "submit"
Then I should see "resultz"

If however this is really what you need, create a step definition "post a lot of vars" and implement the details in your context file. 但是,如果这确实是您需要的,请创建一个步骤定义“发布大量变量”,并在上下文文件中实现细节。

I would use an integration test to do the testing of the controller when it's only an api-endpoint. 当它只是一个API端点时,我将使用集成测试来对控制器进行测试。

Alternatively, you could use the TableNodes (I know I'm a bit late to the party), but in effect, if you use something like this snippet: 或者,您可以使用TableNodes(我知道我来晚了一点),但实际上,如果您使用以下代码段:

    /**
     * @Then /^I have the following param(?:|eter)s with values:$/
     */
    public
    function iHaveTheFollowingParamsWithValues(TableNode $table)
    {
        foreach ($table->getRowsHash() as $param => $value) {
            $this->iHaveParamWithValue($param, $value);
        }
    }

And: 和:

    /**
     * @Then /^I should have the following codes with values:$/
     */
    public
    function iShouldHaveTheFollowingCodesWithValues(TableNode $table)
    {
        foreach ($table->getRowsHash() as $code => $value) {
            $this->iShouldHaveCodeWithValue($code, $value);
        }
    }

It will call your functions stated, and allow you to write the data in a table, as with the examples table of the Scenario Outline. 它将调用陈述的函数,并允许您将数据写入到表中,就像方案大纲的示例表一样。

Such as: 如:

Scenario Outline: Check Api Simple Test
Given  I use http method "POST"

And    I have the following params with values:
       |sEventType|<sEventType>|
       |aFilters[]|<aFilters[]>|
       |nCompany  |<nCompany>  |
..................................................
And    I call url "<path>"

And    I should have the following codes with values:
       |code |<code> |
       |error|<error>|

Examples : 
|path ........
|..............

That should aid with readability, and speed up both test writing and test execution by a small amount. 这将有助于提高可读性,并少量地加快测试编写和测试执行的速度。

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

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