LoveAndHappiness
LoveAndHappiness

Reputation: 10135

Laravel 5: Controller Testing - PHPUnit returns green

the following simple controller test makes a 'GET' request to the PostsController@index action:

<?php 

class PostsControllerTest extends TestCase {

    public function testIndex()
    {
        $response = $this->action('GET', 'PostsController@index');

    }
}

In my understanding, if the index method does not exist in my controller, I shouldn't get the green light, when calling phpunit in my command line.

Yet my controller looks like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    // public function index()
    // {
    //    //
    //    return 'Posts Index';
    //}
}

As you can clearly see the index method is commented out and I still get this:

**OK (1 test, 0 assertions)**

Any suggestions?

Upvotes: 2

Views: 231

Answers (1)

BrokenBinary
BrokenBinary

Reputation: 7889

You aren't making any assertions. Your test isn't checking if $response is "OK".

Change your test to this:

public function testIndex()
{
    $response = $this->action('GET', 'PostsController@index');
    $this->assertEquals(200, $response->status());
}

This test asserts that the page responded with a 200 status code, which means it was successful.

You can read up on Laravel's testing here.

Upvotes: 1

Related Questions