gpt4 book ai didi

php - Laravel 尝试对 API JSON 响应进行单元测试

转载 作者:可可西里 更新时间:2023-10-31 22:11:18 29 4
gpt4 key购买 nike

试图在这里一次学习太多新东西(Laravel、PHPUnit 等),所以这可能只是一个大脑疲惫的问题,仍然希望得到一些帮助。

我有一个非常基本的“博客”项目,使用 Laravel 作为 API 层,使用 AngularJS 作为前端。我想对 API 端点进行单元测试,但在我的测试函数中无法弄清楚如何处理 JSON。

当我尝试运行 testGetBlogPosts() 时,我在 CLI 中看到了类似 JSON 的输出,但我无法执行 json_decode() 并检查对象的某些部分是否符合我的预期结果。在这里,我只是想确保结果数组中第一个对象的 ID 是 ID“1”。

我收到的测试结果是:1) 示例测试::testGetBlogPostsErrorException:试图获取非对象的属性

非常感谢任何帮助或建议!

TL;DR:测试用例未正确处理来自 API 端点的 JSON 响应

Controller

class HomeController extends BaseController {

/*
|--------------------------------------------------------------------------
| Default Home Controller
|--------------------------------------------------------------------------
|
| You may wish to use controllers instead of, or in addition to, Closure
| based routes. That's great! Here is an example controller method to
| get you started. To route to this controller, just add the route:
|
| Route::get('/', 'HomeController@showWelcome');
|
*/
public function showWelcome()
{
return View::make('hello');
}

public function getBlogPosts()
{
$posts = Post::get()->take(5)->toJson();
// echo $posts; PER THE ACCEPTED ANSWER, RETURN NOT ECHO
return $posts;
}
public function getSinglePost($postId)
{
$posts = Post::find($postId)->toJson();
// echo $posts; PER THE ACCEPTED ANSWER, RETURN NOT ECHO
return $posts;
}

}

测试文件

class ExampleTest extends TestCase {

/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}

public function testGetBlogPosts()
{
$response = $this->call('GET', 'api/getBlogPosts');

$array = json_decode($response);
$result = false;
if($array[0]->id == 1)
{
$result = true;
}
$this->assertEquals(true, $result);
}
}

应要求,完整的测试输出

root@homestead:/home/vagrant/Laravel/Homestead/Blog# phpunit PHPUnit 3.7.28 by Sebastian Bergmann.

Configuration read from /home/vagrant/Laravel/Homestead/Blog/phpunit.xml

.E[{"id":"1","user_id":"1","title":"This is a test post","post_body":"testststs","created_at":"2014-08-07 19:26:26","updated_at":"2014-08-07 19:26:26"},{"id":"2","user_id":"75","title":"Libero rerum rem praesentium et et at doloribus asperiores.","post_body":"Commodi aut beatae aut veritatis eum soluta sint. In aut cumque iure quis.","created_at":"2014-08-07 19:26:26","updated_at":"2014-08-07 19:26:26"}]

Time: 1.85 seconds, Memory: 18.50Mb

There was 1 error:

1) ExampleTest::testGetBlogPosts ErrorException: Trying to get property of non-object

/home/vagrant/Laravel/Homestead/Blog/app/tests/ExampleTest.php:22

FAILURES! Tests: 2, Assertions: 1, Errors: 1.

如果我在浏览器中访问这个端点,我会得到这个

[
{
id: 1,
user_id: 1,
title: "This is a test post",
subtitle: "",
post_body: "testststs",
created_at: "2014-08-07 19:26:04",
updated_at: "2014-08-07 19:26:04"
},
{
id: 2,
user_id: 18,
title: "Rem deserunt dolor odit tempore qui eaque labore.",
subtitle: "",
post_body: "Ea a adipisci molestiae vel dignissimos. Ea blanditiis et est.",
created_at: "2014-08-07 19:26:04",
updated_at: "2014-08-07 19:26:04"
}
]

最佳答案

希望你现在已经明白了,但这是我使用的:

$array = json_decode($response->getContent());

关于php - Laravel 尝试对 API JSON 响应进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25190451/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com