编辑:
我遇到了同样的问题,这里有一个有点混乱的解决方案!
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()
,它只是检查是否调用了给定的视图。