解决未定义的变量在使用PHPUnit进行测试的存储()时laravel -4-
我有路由确实向商店一个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": "[email protected]", "name":"Tester"}';
$json = json_decode($jsonString);
$this->client->request('POST', 'foo');
$this->assertEquals($json, $this->client->getResponse());
}
当我在我的cmd中运行phpunit时,出现错误,指出“名称”未定义。我知道我并没有将任何事情传递给请求,所以我肯定没有任何事情正在被检查,但我的问题是我如何确实通过我的JSON字符串来检查?
每次我把$ json放在客户端请求中,它会要求一个数组,但是当我将json字符串转换为数组时,json_decode需要一个字符串。
UPDATE
我与输入数据的传递乱搞,我碰到这个传来:
$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());
但每当我运行测试,我仍然得到“名称:未定义”这是仍然没有通过我创建的输入。
为什么你打开另一个问题,问题是一样的在http://stackoverflow.com/questions/24857763/testing-a-post-using-phpunit-in-laravel-4/24857916#24857916 – Klemen
是的我澄清说,在我的答案当然是我得到一个错误。我的问题是,我如何实际做一个测试,通过示例输入来检查store()是否正在做它应该做的事情。 – sfas
我打开了另一个问题,因为这个问题在我正在寻找的过程中有点特别。另一个问题似乎有点草率。 – sfas