【问题标题】:Laravel - Testing what happens after a redirectLaravel - 测试重定向后会发生什么
【发布时间】:2015-02-01 15:40:55
【问题描述】:

我有一个控制器,它在提交电子邮件后执行重定向到主页,如下所示:

return Redirect::route('home')->with("message", "Ok!");

我正在为它编写测试,但我不确定如何让 phpunit 跟随重定向,以测试成功消息:

public function testMessageSucceeds() {
    $crawler = $this->client->request('POST', '/contact', ['email' => 'test@test.com', 'message' => "lorem ipsum"]);

    $this->assertResponseStatus(302);
    $this->assertRedirectedToRoute('home');

    $message = $crawler->filter('.success-message');

    // Here it fails
    $this->assertCount(1, $message);
}

如果我用控制器上的代码代替它,并删除前 2 个断言,它就可以工作

Session::flash('message', 'Ok!');
return $this->makeView('staticPages.home');

但我想使用Redirect::route。有没有办法让 PHPUnit 跟随重定向?

【问题讨论】:

    标签: php redirect laravel phpunit


    【解决方案1】:

    您可以通过这种方式告诉爬虫遵循重定向:

    $crawler = $this->client->followRedirect();
    

    所以你的情况是这样的:

    public function testMessageSucceeds() {
        $this->client->request('POST', '/contact', ['email' => 'test@test.com', 'message' => "lorem ipsum"]);
    
        $this->assertResponseStatus(302);
        $this->assertRedirectedToRoute('home');
    
        $crawler = $this->client->followRedirect();
    
        $message = $crawler->filter('.success-message');
    
        $this->assertCount(1, $message);
    }
    

    【讨论】:

      【解决方案2】:

      你可以让 PHPUnit 跟随重定向:

      Laravel >= 5.5.19

      $this->followingRedirects();
      

      Laravel :

      $this->followRedirects();
      

      用法:

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

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


      对于这两者之间的版本

      请参阅https://github.com/laravel/framework/issues/18016#issuecomment-322401713 了解解决方法。

      【讨论】:

      • 此功能已从 laravel >= 5.4.12 中删除,您可以使用this 解决方法。
      • @MerhawiFissehaye 那里。更新了答案以包含它。
      【解决方案3】:

      对于 Laravel 5.6,你可以设置

      $protected followRedirects = true;
      

      在您的测试用例的类文件中

      【讨论】:

      • 据我所知,至少在 Laravel 5.8 中,如果你有多个测试,这将不起作用,因为一旦重定向被跟踪,它会将 followRedirects 重置为 false。
      【解决方案4】:

      从 Laravel 5.5 开始测试重定向你可以使用assertRedirect:

      /** @test */
      public function store_creates_claim()
      {
          $response = $this->post(route('claims.store'), [
              'first_name' => 'Joe',
          ]);
      
          $response->assertRedirect(route('claims.index'));
      }
      

      【讨论】:

      • 从 5.5 开始就可以使用了
      【解决方案5】:

      Laravel 8 测试

      $response = $this->post'/contact', ['email' => 'test@test.com', 'message' => "lorem ipsum"]);
      
      $response->assertStatus(302);
      $response->assertRedirect('home');
      
      $this->followRedirects($response)->assertSee('.success-message');
      //or
      $this->followRedirects($response)->assertSee('Ok!');
      

      为我工作,希望对您有所帮助。

      【讨论】:

      • 当您既想测试发生的重定向并验证您在重定向页面上看到的内容时,这非常有用。最好有一个assertRedirectAndFollow() 方法,这样你就可以继续链接了。
      • 找不到followsRedirects方法
      • 没有正确的函数是 "$this->followRedirects()"
      猜你喜欢
      • 2012-05-18
      • 2014-02-05
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      相关资源
      最近更新 更多