Php 拉威尔试验。可以在同一测试中使用多个调用吗?

Php 拉威尔试验。可以在同一测试中使用多个调用吗?,php,laravel,testing,phpunit,Php,Laravel,Testing,Phpunit,我有这个测试: public function test_user_can_access_the_application_page( { $user=[ 'email'=>'user@user.com', 'password'=>'user1234', ]; $response=$this->call('POST','/login',$user); $this-

我有这个测试:

public function test_user_can_access_the_application_page(  
{
        $user=[
            'email'=>'user@user.com',
            'password'=>'user1234',
        ];

        $response=$this->call('POST','/login',$user);
        $this->assertAuthenticated();
        $response->assertStatus(302)
            ->assertRedirect('/dashboard')
            ->assertLocation('/dashboard');
        $response=$this->call('GET','/application/index');
        $response->assertLocation('/application/index');
}
在我登录后,它会引导我进入仪表板,直到现在还可以,但是如果我想在登录后访问其他页面,我不能。这个错误出现了

预期:'http://mock.test/application/index"

实际:'http://mock.test"

在同一测试中不允许多次调用,或者登录后是否有其他方法访问其他页面?
(注意:actingAs无法使用Factorys,因此我需要登录)。

我想您需要以用户身份调用该函数,因为您只能登录访问它。Laravel为此类情况提供了
actingAs()
方法

您可以创建一个随机用户,该用户具有登录您的应用程序的权限,也可以使用种子用户并以所选用户的身份调用函数

$response=$this->actingAs($user)->调用('GET','/application/index')

如果在没有
actingAs()
的情况下调用它,中间件会将您重定向回登录或主屏幕(您在
LoginController
中定义的内容)


在我看来,这个测试用例应该有自己的测试方法。我建议对每个路线或每个用例使用测试方法。它使您的测试安排清晰,易于理解。

如果您希望通过身份验证,最简单的方法是让PHPUnit使用该方法模拟身份验证

此方法会对用户进行身份验证,因此您不想用它测试登录方法。您应该将登录测试与测试其他页面分开编写

要回答您的问题,可以在同一测试中发出多个请求,但在这种情况下,将登录测试链接到“应用程序/索引”页面可能没有多大意义

public function test_the_user_can_login()
{
    $user = [
        'email'=>'user@user.com',
        'password'=>'user1234',
    ];

    $response = $this->call('POST','/login',$user);
    $this->assertAuthenticated();
    $response->assertStatus(302)
             ->assertRedirect('/dashboard')
             ->assertLocation('/dashboard');
}

public function test_user_can_access_the_application_page()
{
    $user = User::where($email, "user@user.com")->first();

    $response = $this->actingAs($user)
                     ->call('GET','/application/index');

    $response->assertLocation('/application/index');
}


如果您不能将工厂用于actingAs,那么您应该尝试使用cookie

看看图书馆