Php Laravel控制器布局测试
我正在尝试在我正在构建的新Laravel应用程序中包含单元测试。 现在我想测试我的OrderController。此控制器的索引方法如下所示:Php Laravel控制器布局测试,php,unit-testing,controller,laravel,laravel-4,Php,Unit Testing,Controller,Laravel,Laravel 4,我正在尝试在我正在构建的新Laravel应用程序中包含单元测试。 现在我想测试我的OrderController。此控制器的索引方法如下所示: public function index() { // We need the extra 'orders()' for the query scope $orders = $this->order->orders()->paginate($this->perPage); $this->layou
public function index()
{
// We need the extra 'orders()' for the query scope
$orders = $this->order->orders()->paginate($this->perPage);
$this->layout->content = View::make('orders.index', compact('orders'));
}
public function testIndex()
{
$this->call('GET', 'orders');
$this->assertResponseOk();
$this->assertViewHas('orders');
}
现在我的测试如下所示:
public function index()
{
// We need the extra 'orders()' for the query scope
$orders = $this->order->orders()->paginate($this->perPage);
$this->layout->content = View::make('orders.index', compact('orders'));
}
public function testIndex()
{
$this->call('GET', 'orders');
$this->assertResponseOk();
$this->assertViewHas('orders');
}
现在,如果我运行phpunit,测试确实会运行,但我得到:1)OrderControllerTest::testIndex
断言数组具有键“orders”失败。
我已经找到了使用控制器布局的问题$this->layout
。
如果我只返回视图::make()测试通过,如果我返回$this->layout…
它也通过,但这会破坏实际的应用程序
所以我找到的唯一选择是使用返回视图::make()
,并在该视图中使用@extends('master')
。但我觉得奇怪的是,如果你想测试它,就不能在你的应用程序中使用控制器布局
我做错什么了吗?编辑:
我也有同样的问题,这里有一个稍微混乱的解决方案
View::composer('ordersviewname', function($view) {
$this->assertArrayHasKey('orders', $view->getData());
});
$this->call('GET', 'orders');
请注意,您必须将此代码放在$this->call()之前
编辑:
这里有更优雅的解决方案!
向TestCase添加函数
protected $nestedViewData = array();
public function registerNestedView($view)
{
View::composer($view, function($view){
$this->nestedViewsData[$view->getName()] = $view->getData();
});
}
/**
* Assert that the given view has a given piece of bound data.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function assertNestedViewHas($view, $key, $value = null)
{
if (is_array($key)) return $this->assertNestedViewHasAll($view, $key);
if ( ! isset($this->nestedViewsData[$view]))
{
return $this->assertTrue(false, 'The view was not called.');
}
$data = $this->nestedViewsData[$view];
if (is_null($value))
{
$this->assertArrayHasKey($key, $data);
}
else
{
if(isset($data[$key]))
$this->assertEquals($value, $data[$key]);
else
return $this->assertTrue(false, 'The View has no bound data with this key.');
}
}
/**
* Assert that the view has a given list of bound data.
*
* @param array $bindings
* @return void
*/
public function assertNestedViewHasAll($view, array $bindings)
{
foreach ($bindings as $key => $value)
{
if (is_int($key))
{
$this->assertNestedViewHas($view, $value);
}
else
{
$this->assertNestedViewHas($view, $key, $value);
}
}
}
public function assertNestedView($view)
{
$this->assertArrayHasKey($view, $this->nestedViewsData);
}
现在你要重写你的测试了
$view='orders';
$this->registerNestedView($view);
$this->call('GET', 'orders');
$this->assertNestedViewHas($view, 'orders');
assertNestedViewHas()
具有与assertViewHas()
相同的功能
我添加了另一个函数assertNestedView()
,该函数只检查是否调用了给定视图。测试失败的原因是Orders.Index视图已呈现到布局中,因此在测试时,Orders变量已呈现且不再存在。我不确定最好的测试方法是什么。。。您可以在单独的测试中检查orders.index视图是否包含orders变量,然后检查组合视图是否包含“content”变量,但这就是我所能想到的。+1-这是一个很好的解决方案,现在可以对TDD进行模板化。你应该考虑把这个作为Laravel 4.1非常优雅的解决方案的建议。我肯定会考虑为拉腊维尔4.1增加一个建议。