【发布时间】: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