无法在laravel中测试控制器?

时间:2014-05-06 05:16:44

标签: php testing laravel

我想了解 laravel 4 中的测试。在测试目录中创建了名为 controller 的文件夹。

添加了代码

<?php
//www/Hututoo/app/tests/controller/PostsController.php

class PostsController extends TestCase {
    public function testIndex(){
        return $this->call('GET', 'testroute');
    }
}

然后在终端中写了phpunit。我希望它显示错误,因为路由文件中不存在testroute

显示:

PHPUnit 4.1.0 by Sebastian Bergmann.

Configuration read from /var/www/Hututoo/phpunit.xml

.

Time: 128 ms, Memory: 5.50Mb

OK (1 test, 1 assertion)

所以它没有显示任何错误。然后,我删除了ExampleTest.php(已经存在于测试文件夹中),因为我认为它可能会阻止测试。这次终端显示了

PHPUnit 4.1.0 by Sebastian Bergmann.

Configuration read from /var/www/Hututoo/phpunit.xml



Time: 42 ms, Memory: 1.50Mb

No tests executed!

那么我做错了什么?

1 个答案:

答案 0 :(得分:0)

你忘了宣称结果。

含义:您需要告诉测试,您期望该路线应该返回什么。如果你看一下示例测试用例,你会看到这一行:

$this->assertTrue($this->client->getResponse()->isOk());

使用该行告诉测试,您希望该路由返回响应,没有任何错误或失败。

您还可以使用以下内容使用HTTP状态代码断言:

$this->assertResponseStatus(200, 'Your custom error message here');

HTTP状态代码200代表“确定”...意味着找到了已经请求的资源,如您所想,如果您希望路由不返回资源,或者找不到资源,或者根本不存在,你可以使用:

$this->assertResponseStatus(404, 'Your custom error message here');

404我们都知道,404是“资源未找到”的状态代码。

以下是状态代码列表:http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

我希望我说清楚或易于理解。