【发布时间】:2014-09-11 13:43:53
【问题描述】:
已解决
我的路由在控制器中向 store() 执行 POST 路由。
我正在尝试测试该操作是否正常工作。
控制器:
public function store() {
$d= Input::json()->all();
//May need to check here if authorized
$foo= new Foo;
$d = array();
$d['name'] = $d['name'];
$d['address'] = $d['address'];
$d['nickname'] = $d['nickname'];
if($foo->validate($d))
{
$new_foo= $foo->create($d);
return Response::json(Foo::where('id','=',$new_foo->id)->first(),200);
}
else
{
return Response::json($foo->errors(),400);
}
}
现在我正在尝试使用一个名为 FooTest.php 的新类来测试它
这是我目前正在尝试执行的使检查工作的功能:
public function testFooCreation()
{
$jsonString = '{"address": "82828282", "email": "test@gmail.com", "name":"Tester"}';
$json = json_decode($jsonString);
$this->client->request('POST', 'foo');
$this->assertEquals($json, $this->client->getResponse());
}
当我在 cmd 中运行 phpunit 时,我收到一条错误消息,指出“名称”未定义。我知道我实际上并没有向请求传递任何东西,所以我很肯定实际上没有任何检查,但我的问题是我如何实际传递我的 json 字符串来检查?
每次我将 $json 放入客户端请求中时,它都会请求一个数组,但是当我将我的 json 字符串转换为数组时,json_decode 需要一个字符串。
更新
我在处理输入数据的传递时遇到了这个问题:
$input = [
'name' => 'TESTNAME',
'address' => '299 TESTville',
'nickname' => 't'
];
Input::replace($input);
Auth::shouldReceive('attempt')
->with(array('name' => Input::get('name'),
'address' => Input::get('address'),
'nickname' => Input::get('nickname')))
->once()
->andReturn(true);
$response = $this->call('POST', 'foo', $input);
$content = $response->getContent();
$data = json_decode($response->getContent());
但是每当我运行测试时,我仍然得到“name:undefined”它仍然没有通过我创建的输入。
【问题讨论】:
标签: php json laravel-4 phpunit