【发布时间】:2015-02-04 18:04:01
【问题描述】:
响应是 JSON 格式,我使用的是定制的 MVC 框架,我不确定请求和响应过程是如何产生的。使用以下语法创建服务方法。
public function getSessionsMethod()
{
// data auto encoded as JSON
return array('hello', 'world');
}
来自 JavaScript 的请求看起来像这样/svc/api/getSessions。
我最初的想法是简单地使用流方法。这种测试形式是否有最佳实践?
public function testCanGetSessionsForAGivenId()
{
$params = http_build_query(
array(
'id' => 3,
)
);
$options = array(
'http' => array(
'method' => 'GET',
'content' => $params,
)
);
$context = stream_context_create($options);
$response = file_get_contents(
'http://vbates/svc/api/getSessions', false, $context
);
$json = json_decode($response);
$this->assertEquals(3, $json->response);
}
【问题讨论】:
标签: phpunit