我正在尝试为Laravel控制器编写一个phpunit测试,该控制器期望使用JSON格式的主体发布请求.
控制器的简化版本:
class Account_Controller extends Base_Controller
{
public $restful = true;
public function post_login()
{
$credentials = Input::json();
return json_encode(array(
'email' => $credentials->email,
'session' => 'random_session_key'
));
}
}
Run Code Online (Sandbox Code Playgroud)
目前我有一个测试方法正确地将数据作为urlencoded表单数据发送,但我无法弄清楚如何将数据作为JSON发送.
我的测试方法(我用的是GitHub的要点在这里写测试时)
class AccountControllerTest extends PHPUnit_Framework_TestCase {
public function testLogin()
{
$post_data = array(
'email' => 'user@example.com',
'password' => 'example_password'
);
Request::foundation()->server->set('REQUEST_METHOD', 'POST');
Request::foundation()->request->add($post_data);
$response = Controller::call('account@login', $post_data);
//check the $response
}
}
Run Code Online (Sandbox Code Playgroud)
我在前端使用angularjs,默认情况下,发送到服务器的请求是JSON格式.我宁愿不改变它发送urlencoded形式.
有谁知道如何编写一个为控制器提供JSON编码体的测试方法?