【问题标题】:Laravel PHPunit redirect to localhostLaravel PHPunit 重定向到本地主机
【发布时间】:2021-07-28 18:26:19
【问题描述】:

我正在尝试使用actingAs (Laravel 5.8) 登录我的测试,但它似乎没有做太多,因为它不断将我重定向到本地主机。不知道它是从哪里得到的,因为我没有重定向到任何地方的本地主机,它至少应该重定向到我的应用程序 url?

 public function testCategoriesIndex()
{
    $response = $this->actingAs($this->superAdminUser)
                        ->withSession(['companyId' => $this->testCompany->id])
                        ->get(url("/{$this->routePrefix}/categories{$this->urlParameters}"));       

    dd($response->content());
    
    $response->assertStatus(200);
}

输出

<!DOCTYPE html>\n
<html>\n
    <head>\n
        <meta charset="UTF-8" />\n
        <meta http-equiv="refresh" content="0;url='http://localhost/login'" />\n
\n
        <title>Redirecting to http://localhost/login</title>\n
    </head>\n
    <body>\n
        Redirecting to <a href="http://localhost/login">http://localhost/login</a>.\n
    </body>\n
</html>

文档也没有提到任何关于这些东西的任何内容,还不如空着...... https://laravel.com/docs/5.8/testing

【问题讨论】:

    标签: laravel-5 phpunit


    【解决方案1】:

    你可以让 PHPUnit 跟随重定向:

    Laravel >= 5.5.19:

    $this-&gt;followingRedirects();

    Laravel

    $this-&gt;followRedirects();

    用法:

    $response = $this->followingRedirects()
    
    ->post('/login', ['email' => 'john@example.com'])
    
    ->assertStatus(200);
    

    注意:这需要为每个请求明确设置。

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 2014-03-11
      • 2012-03-29
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多