我全部,我正在努力获得适当的TDD,但这并非一帆风顺。
这个测试一直在失败,我无法弄清楚如何让它通过。
#>phpunit
PHPUnit 3.7.20 by Sebastian Bergmann.
Configuration read from /Users/ghall/Sites/laravel.hyundianet.hyundai.co.nz/phpunit.xml
<h2>Charts</h2>
<table class="table table-striped">
{"error":{"type":"ErrorException","message":"Invalid argument supplied for foreach()","file":"\/Users\/ghall\/Sites\/laravel.dev\/app\/storage\/views\/9fe7adcadbe887c8bce1c18e06defac2","line":8}}
我看起来是因为它试图渲染出一些不会从它们中退出的视图
路线
App::bind('App\Models\Interfaces\ChartInterface', 'App\Models\Repositories\EloquentChartRepository');
Route::resource('chart', 'ChartController');
控制器
use App\Models\Interfaces\ChartInterface;
use App\Models\Repositories\EloquentChartRepository;
class ChartController extends BaseController
{
protected $layout = 'layouts.application';
protected $chart;
function __construct(ChartInterface $chart)
{
$this->chart = $chart;
// var_dump($chart);
}
public function index()
{
$charts = $this->chart->all();
$this->layout->content = View::make('chart.index')
->with('charts', $charts);
}
}
存储库
namespace App\Models\Repositories;
use App\Models\Interfaces\ChartInterface;
use Chart;
class EloquentChartRepository implements ChartInterface
{
public function all()
{
return Chart::all();
}
public function find($id)
{
return Chart::find($id);
}
}
接口
namespace App\Models\Interfaces;
interface ChartInterface
{
public function all();
public function find($id);
}
测试
class ChartControllerTest extends TestCase
{
public function __construct()
{
$this->mock = Mockery::mock('App\Models\Interfaces\ChartInterface');
}
public function tearDown()
{
Mockery::close();
}
public function testIndex()
{
$this->mock
->shouldReceive('all')
->once()
->andReturn('charts');
$this->app->instance('App\Models\Interfaces\ChartInterface', $this->mock);
$this->call('GET', 'chart');
$this->assertViewHas('charts');
}
}
查看
@if (!empty($charts))
@section('content')
<h2>Charts</h2>
<table class="table table-striped">
@foreach ($charts as $chart)
<tr>
<td>{{ $chart->id }}</td>
<td>{{ $chart->report_name }}</td>
<td>{{ $chart->description }}</td>
<td>{{ $chart->graphtype }}</td>
<td>{{ $chart->date_range }}</td>
<td>{{ $chart->user }}</td>
</tr>
@endforeach
</table>
@stop
@else
@section('content')
<h2>Nothing to display</h2>
@stop
@endif
新 我发现了问题,它在Controller / view中。当测试调用控制器时,控制器返回带有标记的视图。
如果我将控制器更改为然后它可以工作,但这不是一个好的解决方案。
public function index()
{
$charts = $this->chart->all();
if(is_object($charts)){
$this->layout->content = View::make('chart.index')
->with('charts', $charts);
return;
}
return View::make('chart.test')->with('charts', $charts);
}
答案 0 :(得分:2)
在ChartControllerTest内部,在测试testIndex()中,mock返回一个String,视图需要一个数组。试试这样的事情:
$this->mock
->shouldReceive('all')
->once()
->andReturn(array());
答案 1 :(得分:0)
您可以使用is_array函数检查提供给视图的变量是否为数组,例如在您的视图中执行
@if(is_array($charts))
@foreach ($charts as $chart)
<tr>
<td>{{ $chart->id }}</td>
<td>{{ $chart->report_name }}</td>
<td>{{ $chart->description }}</td>
<td>{{ $chart->graphtype }}</td>
<td>{{ $chart->date_range }}</td>
<td>{{ $chart->user }}</td>
</tr>
@endforeach
@endif
此致