在laravel中对REST API进行单元测试:忽略基本身份验证

时间:2014-09-18 14:07:34

标签: php unit-testing laravel phpunit

我想用基本身份验证测试一个小API。

路线看起来像这样:

Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
    Route::get('/', function()
    {
        return 'Hello';
    });
});

测试看起来像那样:

class APITest extends TestCase {
    public function testHTTPAuthFailed()
    {
        $response = $this->call('GET', 'api/v1');
        fwrite(STDERR, print_r($response->getContent(), TRUE));
    }

}

当我用phpunit运行测试时,我可以看到" Hello"在控制台。它不是我排除的,我以为我会看到"身份验证失败"。但是,如果我尝试使用cURL(在终端上)调用我的API,我可以看到身份验证失败(所以它没问题,它是我的例外情况):

$ curl -i host/api/v1
HTTP/1.1 401 Unauthorized

那么为什么phpunit在laravel(4.2)中跳过(?)基本身份验证。如何测试无效凭证?

1 个答案:

答案 0 :(得分:0)

我和juste在another question找到了答案。我需要在我的测试功能中添加:Route::enableFilters();并且没问题。