doujiao1984
2017-05-29 22:50
浏览 157
已采纳

Laravel测试用例测试重定向到某个路由

I'm writing some test cases for my Laravel app with PHPUnit.

Below is the class that is producing the error:

<?php

namespace Test\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class UserLoginTest extends TestCase
{
    use DatabaseMigrations;

    public function setUp()
    {
        parent::setUp();
        $this->admin = factory(User::class)->create([
                'is_admin'  => true,
                'password'  => bcrypt('secret'),
            ]);
    }

    /** @test */
    public function test_login_user_form()
    {
        $this->get('/login')
            ->assertStatus(200);
    }

    /** @test */
    public function test_login_user_form_submission()
    {
        $this->post('/login', [
                'email'     => $this->admin->email,
                'password'  => 'secret',
            ]);

        $this->assertRedirectedTo('/');
    }
}

The problem is that when I run PHPUnit I get this error:

PHPUnit 5.7.20 by Sebastian Bergmann and contributors. ....E.. 7 / 7 (100%) Time: 1.65 seconds, Memory: 20.00MB There was 1 error: 1) Test\Feature\UserLoginTest::test_login_user_form_submission Error: Call to undefined method Test\Feature\UserLoginTest::assertRedirectedTo() /Users/shivampaw/Desktop/valet/UltimateCRM/tests/Feature/UserLoginTest.php:37

It says that assertRedirectedTo is an undefined method but I do not have any clue why. I have tried methods like assertRedirect but cannot get this working!

图片转代码服务由CSDN问答提供 功能建议

我正在使用PHPUnit为我的Laravel应用编写一些测试用例。

下面是产生错误的类:

 &lt;?php 
 
namespace Test \ Feature; 
 
use Tests \ TestCase; 
use App \ Models  \ user; 
use Illuminate \ Foundation \ Testing \ DatabaseMigrations; 
 
class UserLoginTest扩展TestCase 
 {
使用DatabaseMigrations; 
 
公共函数setUp()
 {
 parent :: setUp(); \  n $ this-&gt; admin = factory(User :: class) - &gt; create([
'is_admin'=&gt; true,
'password'=&gt; bcrypt('secret'),
])  ; 
} 
 
 / ** @test * / 
公共函数test_login_user_form()
 {
 $ this-&gt; get('/ login')
  - &gt; assertStatus(200); \  n} 
 
 / ** @test * / 
公共函数test_login_user_form_submission()
 {
 $ this-&gt; post('/ login',[
'email'=&gt; $ this-&gt  ; admin-&gt;电子邮件,
'密码'=&gt;  'secret',
]); 
 
 $ this-&gt; assertRedirectedTo('/'); 
} 
} 
   
 
 

问题 就是当我运行PHPUnit时,我得到了这个错误:

Sebastian Bergmann和贡献者的PHPUnit 5.7.20。 .... E .. 7 / 7(100%) 时间:1.65秒,内存:20.00MB 有1个错误: 1)Test \ Feature \ UserLoginTest :: test_login_user_form_submission错误: 调用未定义的方法 Test \ Feature \ UserLoginTest :: assertRedirectedTo() /Users/shivampaw/Desktop/valet/UltimateCRM/tests/Feature/UserLoginTest.php:37

nn

它说 assertRedirectedTo 是一个未定义的方法,但我没有任何线索的原因。 我尝试过像 assertRedirect 这样的方法,但是无法正常工作!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题