Laravel 测试,获取 JSON 内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32208535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel testing, get JSON content
提问by rap-2-h
In Laravel's unit test, I can test a JSON API like that:
在 Laravel 的单元测试中,我可以测试这样的 JSON API:
$this->post('/user', ['name' => 'Sally'])
->seeJson([
'created' => true,
]);
But what if I want to use the response. How can I get the JSON response (as an array) using $this->post()
?
但是如果我想使用响应怎么办。如何使用 获取 JSON 响应(作为数组)$this->post()
?
采纳答案by Mike McLin
Currently in 5.3 this is working...
目前在 5.3 这正在工作......
$content = $this->get('/v1/users/1')->response->getContent()
;
$content = $this->get('/v1/users/1')->response->getContent()
;
It does break the chain, however since response
returns the response and not the test runner. So, you should make your chainable assertions before fetching the response, like so...
它确实打破了链条,但是因为response
返回响应而不是测试运行器。所以,你应该在获取响应之前做出可链接的断言,就像这样......
$content = $this->get('/v1/users/1')->seeStatusCode(200)->response->getContent()
;
$content = $this->get('/v1/users/1')->seeStatusCode(200)->response->getContent()
;
回答by Jan Tlapák
Proper way to get the content is:
获取内容的正确方法是:
$content = $this->get('/v1/users/1')->decodeResponseJson();
回答by cmac
I like to use the json method when working with json, instead of ->get()
我在处理json时喜欢使用json方法,而不是->get()
$data = $this->json('GET', $url)->seeStatusCode(200)->decodeResponseJson();
回答by Daniel
I hit a similar problem and could not get $this->getResponse()->getContent() working with the built in $this->get() method. I tried several variations with no success.
我遇到了类似的问题,无法使用内置的 $this->get() 方法使 $this->getResponse()->getContent() 工作。我尝试了几种变体,但都没有成功。
Instead I had to change the call to return the full http response and get the content out of that.
相反,我不得不更改调用以返回完整的 http 响应并从中获取内容。
// Original (not working)
$content = $this->get('/v1/users/1')->getContent();
// New (working)
$content = $this->call('GET', '/v1/users/1')->getContent();
回答by Mwthreex
Simple way:
简单的方法:
$this->getJson('api/threads')->content()
回答by Edgar Kalema
As at Laravel 6, this worked for me.
I was returning an automatically generated field (balance) after the POST request has created the entity.
The response was in the structure {"attributes":{"balance":12345}}
在 Laravel 6 中,这对我有用。在 POST 请求创建实体后,我返回了一个自动生成的字段(余额)。响应在结构中{"attributes":{"balance":12345}}
$response = $this->postJson('api/v1/authors', [
'firstName' => 'John',
'lastName' => 'Doe',
])->assertStatus(201);
$balance = $response->decodeResponseJson()['attributes']['balance'];
decodeResponseJson
will pick the response and transform it to an array for manipulation.
Using getContent()
returns json and you will have to use json_decode
on the returned data to turn it into an array.
decodeResponseJson
将选择响应并将其转换为数组以进行操作。使用getContent()
返回 json,您必须使用json_decode
返回的数据将其转换为数组。
回答by Shadman
Just want to share, I have used the same in $this->json()
like:
只是想分享,我用过同样的$this->json()
:
$response = $this->json('POST', '/order', $data)->response->getContent();
But I added one more line to use json response and decode otherwise decodeResponseJson()
was not working for me.
但是我又添加了一行来使用 json 响应和解码,否则decodeResponseJson()
对我不起作用。
$json = json_decode($response);
回答by Tobi
Found a better way:
找到了更好的方法:
$response = $this->json('POST', '/order', $data)->response->getOriginalContent();
This method returns the response json as an array.
此方法将响应 json 作为数组返回。