簡體   English   中英

Laravel 4 TDD測試失敗

[英]Laravel 4 TDD Test failing

我全部,我正在努力獲得適當的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

我發現問題,它在控制器/視圖中。 當測試調用控制器時,控制器返回帶有標記的視圖。

如果我將控制器更改為然后它可以工作,但這不是一個好的解決方案。

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

在ChartControllerTest內部,在測試testIndex()中,mock返回一個String,視圖需要一個數組。 試試這樣的事情:

 $this->mock
        ->shouldReceive('all')
        ->once()
        ->andReturn(array());

您可以使用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

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM