M
M
Muhammad2015-07-29 14:53:21
Unit testing
Muhammad, 2015-07-29 14:53:21

Testing Laravel 5.1 View facade?

There is a getIndex method for HomeController:

public function getIndex()
{
    return \View::make('home');
}

In routes:
Route::get('/', ['as' => 'home', 'uses' => '[email protected]']);

And the simplest test for it:
<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class HomeControllerTest extends TestCase
{
    public function testIndexPage()
    {
    	\View::shouldReceive('make')->with('home')->andReturn('test');

    	$response = $this->action('GET', '[email protected]');

    	$this->assertResponseOk();
    	$this->assertEquals('test', $response->content());
    }
}

But for some reason it returns the 500th error:
62d3baff5bb043a2a7086429a544fb4d.png
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2015-07-29
@muhammad_97

I tested this code on L5:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class HomeControllerTest extends TestCase
{
    public function testIndexPage()
    {
      \View::shouldReceive('make')->with('home')->andReturn('test');
      View::shouldReceive('share');

      $response = $this->action('GET', '[email protected]');

      $this->assertResponseOk();
      $this->assertEquals('test', $response->getContent());
    }
}

Errors that were:
- You need to mock the share method, it is called somewhere.
In general, I see no reason to test the View facade - there is nothing to test there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question