【发布时间】:2015-08-26 15:24:24
【问题描述】:
我在 Laravel (Dingo) 中构建了一个 API,它运行良好。但是我在实现 phpunit 来对我的 API 进行单元测试时遇到问题
class ProductControllerTest extends TestCase
{
public function testInsertProductCase()
{
$data = array(
, "description" => "Expensive Pen"
, "price" => 100
);
$server = array();
$this->be($this->apiUser);
$this->response = $this->call('POST', '/products', [], [], $server, json_encode($data));
$this->assertTrue($this->response->isOk());
$this->assertJson($this->response->getContent());
}
}
同时我的 API 端点指向这个控制器函数
private function store()
{
// This always returns null
$shortInput = Input::only("price");
$rules = [
"price" => ["required"]
];
$validator = Validator::make($shortInput, $rules);
// the code continues
...
}
但它总是失败,因为 API 无法识别有效负载。 Input::getContent() 返回 JSON,但 Input::only() 返回空白。进一步调查这是因为 Input::only() 仅在请求有效负载的内容类型为 JSON 时才返回值
那么...如何设置我上面的 phpunit 代码以使用 content-type application/json ?我假设它一定与$server 有关,但我不知道是什么
编辑: 我原来的想法其实有2个问题
- Input::getContent() 有效,因为我填写了第六个参数,但 Input::only() 无效,因为我没有填写第三个参数。感谢@shaddy
- 如何在 phpunit 请求标头中设置 content-type 仍未得到解答
谢谢大家
【问题讨论】:
标签: rest laravel phpunit dingo-api