【发布时间】:2014-05-11 08:00:33
【问题描述】:
我正在使用application/json 标头来控制我的控制器在收到请求时如何操作。我需要在我的单元测试中的 POST 包含一个 application/json 标头。
我试过了:
public function testStore()
{
$this->validator
->shouldReceive('validate')
->once()
->with($this->attributes)
->andReturn(true);
$this->repository
->shouldReceive('create')
->once()
->with($this->attributes)
->andReturn($this->entity);
$this->controller
->shouldReceive('creationSucceeded')
->once()
->with($this->entity);
$this->client->request('POST', 'shared/users', [], [], [
'HTTP_CONTENT_TYPE' => 'application/json'
], json_encode($this->attributes));
$this->assertResponseStatus(201);
}
而且我的控制器中的Request::isJson() 继续返回false。
我也尝试使用'CONTENT_TYPE' => 'application/json' 代替上面的HTTP_CONTENT_TYPE。
【问题讨论】:
-
对不起没有实现测试方法..检查这个链接,它可能会有所帮助。stackoverflow.com/questions/20093897/…
-
@ytsejam - 感谢发帖。不幸的是,使用
$this->client->setServerParameter('HTTP_CONTENT_TYPE', 'application/json');对我不起作用,无论是 GET、DELETE、POST 还是 PUT 请求。 -
您的共享/用户看起来如何?请求可以不绑定到响应 http-content-wise。
-
HTTP_Content-Type 怎么样?
-
@kapad 查看这个问题的答案
标签: php unit-testing laravel phpunit