gpt4 book ai didi

json - Laravel 5 PHPUnit 测试 json 后

转载 作者:行者123 更新时间:2023-11-28 20:14:24 25 4
gpt4 key购买 nike

我正在尝试测试 officiel doc 之后的 JSON API。我知道问题出在给 POST 请求的数据数组。该测试适用于像 ['hello' => 'world'] 这样的单级数组,所以显然 post 函数无法处理复杂的结构?我在这里做错了什么?

测试:

public function testInsert()
{
$this->post(
'/test',
[
'content' => 'Hello world!',
'count' => [
'a' => 12.345678,
'b' => 12.345678
],
'user' => [
'id' => 1
]
],
['contentType' => 'application/json']
)->seeJsonEquals([
'status' => true
]);
}

错误:

unknown:myapp nobody$ phpunit
PHPUnit 4.8.24 by Sebastian Bergmann and contributors.

E.

Time: 648 ms, Memory: 15.25Mb

There was 1 error:

1) APITest::testInsert
ErrorException: Invalid argument supplied for foreach()

/Users/nobody/myapp/vendor/laravel/framework/src/Illuminate/Support/Arr.php:487
/Users/nobody/myapp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:231
/Users/nobody/myapp/tests/APITest.php:43

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

Controller :

public function store() {
$user = User::find(Input::get('user.id'));

// TODO: Validate input using JSON schema

if (empty($user))
$errors[] = 'User does not exist.';

if (empty(Input::get('content')))
$errors[] = 'Content is empty.';

$a = Input::get('location.latitude');
if ($a < 15)
$errors[] = 'A out of range.';

$b = Input::get('location.longitude');
if ($b < 20)
$errors[] = 'B out of range.';

if (empty($errors)) {
$post = new Post();

$post->content = Input::get('content');
$post->count()->create([
'a' => $a,
'b' => $b
]);

$user->posts()->save($user);

$post->save();

return APIUtils::makeStatusResponse(true);
}

return APIUtils::makeStatusResponse(false, $errors);
}

最佳答案

我使用 laravel new myapp 命令重新安装了我的 Laravel 应用程序,现在一切正常。我假设问题出在之前使用 composer create-project --prefer-dist laravel/laravel may 进行的安装。

关于json - Laravel 5 PHPUnit 测试 json 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36483956/

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