控制器测试- PHPUnit返回绿色


Laravel 5: Controller Testing - PHPUnit returns green

下面这个简单的控制器测试向PostsController@index动作发出一个'GET'请求:

<?php 
class PostsControllerTest extends TestCase {
    public function testIndex()
    {
        $response = $this->action('GET', 'PostsController@index');
    }
}

在我的理解中,如果索引方法在我的控制器中不存在,当我在命令行中调用phpunit时,我不应该得到绿灯。

但是我的控制器看起来是这样的:

<?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';
    //}
}

你可以清楚地看到index方法被注释掉了,我仍然得到这个:

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

有什么建议吗?

你没有做任何断言。您的测试没有检查$response是否"OK"。

把你的测试改成:

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

这个测试断言页面响应了一个200状态码,这意味着它是成功的。

你可以在这里阅读Laravel的测试