简体   繁体   中英

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 :

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.

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.

If you want these post vars posted, just visit(the url), optionally fill in the form and then submit. 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.

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:

    /**
     * @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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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