【问题标题】:How to replace URL and pass data in behat如何替换 URL 并传递数据
【发布时间】:2017-02-06 17:23:13
【问题描述】:

我只是在学习行为,如果这是非常基本的,请道歉。我有这样的场景:

Scenario: Create Task
    Given I have the JSON payload:
    """
    {
            "task_list_id" : 3,
            "title" : "From Behat",
            "display_order" : 1
    }
    """
    When I send a POST request to task
    Then one SQL ident is created

Scenario: Get the Task
  When I send a GET request to "tasklist/{id}/tasks"
  Then The response code should be 200
  And The response content type should be "application/json"

所以第一个场景建立了连接,然后 JSON 返回一个整数值。我现在希望将 那个 值替换到 URL 具有 {id} 占位符的下一个场景中。

我尝试在第一个场景的 FeatureContext.php 文件中将 $this->output 设置为正文(返回的整数),然后在第二个场景中执行 preg_replace 以将 {id} 更改为整数。看来,当第二个场景运行时,输出在调用该场景之前被清空。

这些是我上面的上下文方法:

  /**
   * @Then One SQL ident is created
   */
  public function theResponseBodyShouldBeAnInteger() {
    $this->theResponseContentTypeShouldBe('application/json');
    $this->theResponseCodeShouldBe(201);

      $body = $this->response->getBody()->getContents();
    if (!ctype_digit($body)) {
            throw New Exception(sprintf('Expected integer response but got "%s".', $body));
    }

    $this->output = $body;
    echo "Output is '$this->output'\n";
  }

  /**
   * @When I send a :method request to :uri
   *
   * @param $method
   * @param $uri
   */
  public function iSendARequestTo($method, $uri)
  {
    echo "Output is '$this->output'\n";
    $uri = str_replace('{id}', $this->output, $uri);

    try {
            if ($method == 'POST' || $method == 'PATCH') {
                    $this->response = $this->client->request($method, $uri, ['json' => $this->requestPayload]);
            } else {
                    $this->response = $this->client->request($method, $uri);
            }
    } catch (GuzzleHttp\Exception\ClientException $ex) {
            throw new Exception($uri . "\n" . $ex->getResponse()->getBody()->getContents());
    }
  }

【问题讨论】:

    标签: behat


    【解决方案1】:

    当然是空白,场景是并且应该是独立的,你在第一个场景中保存的任何内容都会在场景完成后丢失。

    这样做的一种方法是写入文件并从文件中读取。

    无论如何,我看到第一个场景的验证包括第二个场景的验证,唯一的区别是在第一个场景中您正在保存响应的正文,这不是一个好主意,也不是保存的好习惯验证步骤中的数据。

    尽量重复使用。

    场景保持独立。您应该能够在没有第一个场景的情况下运行第二个场景。

    高级示例,仅供参考:

    Scenario: Api - check response of the created task
       Given I have the JSON payload
       When I create a task using POST request -> create request, make sure is successful and save response
       And I send a GET request to created task -> use saved response to do what you need
       Then the response code should be 200
       And the response code should be "application/json" 

    另一个例子:

    Scenario: Api - check response of the created task
       Given I have have a task based on JSON: -> create request, make sure is successful and save response
       When I send a GET request to created task -> use saved response to do what you need
       Then the response code should be 200
       And the response code should be "application/json" 

    这些只是一些例子,我不知道功能,我相信你可以做得更好,总是在创建步骤时牢记你的项目业务语言,以便以后理解它们。

    taskList 方法 你需要确保你有一个 taskList,这是你需要的,你需要检查一个 taskList 是否可用(例如按标题),如果是,那么什么都不做创建任务列表。

    【讨论】:

    • 好的,让我换个方式问这个问题。我正在测试我的 API,所以第一个场景说创建一个具有特定数据的对象。因此,我的下一个场景是想要查询新创建的对象并验证存储的内容是我发送的内容。
    • 谢谢,您更新的示例很有意义。感谢您的帮助。
    • 好的,还是有点挣扎。假设我正在测试我的task API。任务必须位于taskList 内。所以要创建一个任务,我必须创建一个任务列表。这意味着我想对任务做某事的每个场景,我都必须首先创建一个任务列表,以便我可以创建任务,然后针对任务进行测试?这没有任何意义,我不能为整个功能文件创建一个任务列表,然后利用那个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多