0

私はすべて、適切な 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

NEW 問題が見つかりました。それはコントローラー/ビューにあります。テストがコントローラーを呼び出すと、コントローラーはマークアップ付きのビューを返します。

コントローラーを変更すると動作しますが、これは良い解決策ではありません。

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);
    } 
4

2 に答える 2

0

ビューに提供された変数が is_array 関数を使用して配列であるかどうかを確認できます。たとえば、ビューで do

@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

よろしく

于 2013-09-11T16:28:25.197 に答える