【问题标题】:Laravel 5: Controller Testing - PHPUnit returns greenLaravel 5:控制器测试 - PHPUnit 返回绿色
【发布时间】:2015-07-20 02:30:22
【问题描述】:

以下简单的控制器测试向 PostsController@index 操作发出“GET”请求:

<?php 

class PostsControllerTest extends TestCase {

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

    }
}

据我了解,如果我的控制器中不存在 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)**

有什么建议吗?

【问题讨论】:

    标签: php unit-testing laravel testing


    【解决方案1】:

    你没有做出任何断言。您的测试没有检查 $response 是否“正常”。

    把你的测试改成这样:

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

    此测试断言页面以200 status code 响应,这意味着它是成功的。

    你可以阅读 Laravel 的测试here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-31
      • 2020-12-09
      • 2020-05-07
      • 2014-07-26
      • 2015-06-10
      • 2016-06-04
      • 2014-06-06
      • 2015-03-24
      相关资源
      最近更新 更多