php - 为什么我会收到错误 Failed asserting that Response Object (...) is an instance of class "RedirectResponse"?

标签 php laravel-4 phpunit

问题

PHPUnit 断言 $this->assertRedirectedToAction('CatsController@index') 失败。

路线

Route::get('/','CatsController@index');

测试用例

class CatTest extends TestCase {

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testHomePageRedirection()
    {
        $this->call('GET','/');
        $this->assertRedirectedToAction('CatsController@index');

    }

错误

Failed asserting that Illuminate\Http\Response Object (...) is an instance of class "Illuminate\Http\RedirectResponse".

/Applications/MAMP/htdocs/crudapp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/AssertionsTrait.php:110
/Applications/MAMP/htdocs/crudapp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/AssertionsTrait.php:140
/Applications/MAMP/htdocs/crudapp/app/tests/CatTest.php:13

有人可以帮我确定为什么我会遇到这个错误吗?

最佳答案

因为您没有被重定向CatsController@index - 那就是您所在的页面。

Route::get('/','CatsController@index')
Route::get('/redirect', function () {
     return Redirect::action('CatsController@index');
});

public function testHomePageOk()
{
    $this->call('GET','/');
    $this->assertResponseOk(); 
}


public function testRedirectToHomePage()
{
    $this->call('GET','/redirect');
    $this->assertRedirectedToAction('CatsController@index');
}

关于php - 为什么我会收到错误 Failed asserting that Response Object (...) is an instance of class "RedirectResponse"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27760654/

相关文章:

php - HTML - 包含特殊字符时不发送表单字段

javascript - 无法使用 Node.js 处理 PHP 文件

laravel - Eloquent:使用另一列作为关系的主键

caching - Redis 缓存驱动程序在 PHPUnit 中抛出异常

phpunit 和 symfony2 : how to assert number of queries from client or response?

php - 如何获得一定半径的地方

php - 我如何使用php将图像从mysql显示到表

php - Laravel 尝试对 API JSON 响应进行单元测试

laravel-4 - 如何在 laravel 4 中同时使用 Eager Loading 和 Join in Eloquent

mysql - Eloquent ORM 错误 : Column not found: 1054