以下简单的控制器测试使得' GET'请求PostsController @ index action:
<?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';
//}
}
正如您可以清楚地看到索引方法被注释掉了,我仍然得到了这个:
**OK (1 test, 0 assertions)**
有什么建议吗?
答案 0 :(得分:1)
你没有做出任何断言。您的测试未检查$response
是否为&#34;确定&#34;。
将测试更改为:
public function testIndex()
{
$response = $this->action('GET', 'PostsController@index');
$this->assertEquals(200, $response->status());
}
此测试断言页面以200 status code响应,这意味着它成功。
您可以阅读Laravel的测试here。