【发布时间】:2016-03-07 09:53:33
【问题描述】:
我目前正在 Cakephp 3 中编写一个 RESTful API,我需要通过 http://host.com/api/pictures 测试一个 POST 操作。测试代码:
<?php
namespace App\Test\TestCase\Controller;
use App\Controller\Api\UsersController;
use Cake\TestSuite\IntegrationTestCase;
use Cake\Network\Http\Client;
use Cake\Network\Http\FormData;
class ApiPicturesControllerTest extends IntegrationTestCase{
public $fixtures = [
'app.users',
'app.comments',
'app.albums',
'app.users_albums'
];
public function testAdd(){
// $data = new FormData();
$accessToken ='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjksImV4cCI6MTQ1NzYyOTU3NH0.NnjXWEQCno3PUiwHhnUCBjiknR-NlmT42oPLA5KhuYo';
$http = new Client([
'headers' => ['Authorization' => 'Bearer ' . $accessToken, 'Content-Type' => 'application/json']
]);
$data = [
"album_id" => 1,
"link" => "http://www.google.com",
"description" => "testtesttest",
"favorite" => true
];
$result = $http->post('http://vecto.app/api/pictures/add.json', $data, ['type'=>'json']);
// $this->assertResponseOk();
// debug($result);
}
}
当我尝试调试结果时,我得到一个“无法添加或更新子行”,而我确定响应 ID 确实存在
(固定装置也有 id)。此外,日志表明它只尝试插入创建/更新行。因此,我很确定数据被忽略了,但是我找不到解决方案。我已经尝试过不同的标头组合,例如仅用于 Accept 的 application/json,用于 Content-Type 的 application/json 等。我正在使用 Cakephp 的 CRUD 插件将数据传递给 add 函数。
邮递员输出
此外,我尝试使用 Postman Chrome 插件来保存数据,并且确实有效。有谁知道我在测试中做错了什么?
【问题讨论】:
标签: http phpunit cakephp-3.0 postman