简体   繁体   中英

Laravel 4 TDD Test failing

I All, I am trying really had to get in proper TDD but it is not smooth sailing.

This test keeps on failing and I can't figure out how to make it pass.

#>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}}

I appears it's because the view it trying to render out something that does not exits from them mock

Route

App::bind('App\Models\Interfaces\ChartInterface', 'App\Models\Repositories\EloquentChartRepository');

Route::resource('chart', 'ChartController');

Controller

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

Repository

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

Interface

namespace App\Models\Interfaces;

interface ChartInterface
{
    public function all();

    public function find($id);
}

Test

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

View

@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 I found the problem and it's in the Controller / view. When the test call the controller the controller returns the view with markup.

If I change the controller to then it works but this is not a good solution.

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

Inside of ChartControllerTest, in the test testIndex() the mock is returning a String and the view needs a array. Try with something like this:

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

You can check if variable supplied to view is array by using is_array function eg in your view 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

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM