我正在尝试从带有Lumen响应的单元测试中获取响应内容作为字符串:
class MovieQueryTest extends TestCase
{
use DatabaseMigrations;
public function testCanSearch()
{
Movie::create([
'name' => 'Fast & Furious 8',
'alias' => 'Fast and Furious 8',
'year' => 2016
]);
$response = $this->post('/graphql', [
'query' => '{movies(search: "Fast & Furious"){data{name}}}'
]);
$response->getContent(); // Error: Call to undefined method MovieQueryTest::getContent()
$response->getOriginalContent(); // Error: Call to undefined method MovieQueryTest::getOriginalContent()
$response->content; // ErrorException: Undefined property: MovieQueryTest::$content
}
}
但是我无法弄清楚如何获得响应内容。
我不想使用流明TestCase->seeJson()
方法。
我只需要获取响应内容。
答案 0 :(得分:1)
$response
还包含一个response
字段,您需要在该字段上调用getContent()
,因此您首先需要提取该字段,然后调用getContent()
,因此在代码中这将变成:
public function testCanSearch()
{
Movie::create([
'name' => 'Fast & Furious 8',
'alias' => 'Fast and Furious 8',
'year' => 2016
]);
$response = $this->post('/graphql', [
'query' => '{movies(search: "Fast & Furious"){data{name}}}'
]);
$extractedResponse = $response->response; // Extract the response object
$responseContent = $extractedResponse->getContent(); // Extract the content from the response object
$responseContent = $response->response->getContent(); // Alternative one-liner
}