Php Laravel测试在AssertSession完成后是否可以进行以下重定向
我在laravel进行测试,昨天看到在assertSessionHas之前不能有以下重定向。所以我想知道,在assertSessionHas之后,是否可以在不重复整个测试代码的情况下使用它。我现在有一个临时解决办法:Php Laravel测试在AssertSession完成后是否可以进行以下重定向,php,laravel,phpunit,Php,Laravel,Phpunit,我在laravel进行测试,昨天看到在assertSessionHas之前不能有以下重定向。所以我想知道,在assertSessionHas之后,是否可以在不重复整个测试代码的情况下使用它。我现在有一个临时解决办法: $response = $this->post('/signup', [ 'username' => 'Testing', 'email' => 'testing@test.com', 'password' =>
$response = $this->post('/signup', [
'username' => 'Testing',
'email' => 'testing@test.com',
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$this->assertDatabaseHas('users', ['username' => 'Testing', 'email' => 'testing@test.com']);
$response->assertSessionHas('success', 'Your account has been created!');
$code = $this->followRedirects($response)->getStatusCode();
$this->assertEquals(200, $code);
但是我想知道您是否能够稍后使用以下重定向来更改响应,而不是以这种方式使用它。当使用PHPUnit在Laravel中发送post请求时,您不需要传递
csrf\u令牌()
,它是自动包含的
我会像这样重构您的代码:
$this->post('/signup', [
'username' => 'Testing',
'email' => 'testing@test.com',
'password' => 'secret',
'password_confirmation' => 'secret',
])->assertStatus(200)
->assertSessionHas('success', 'Your account has been created!');
$user = User::latest()->first();
$this->assertEquals('Testing', $user->name);
$this->assertEquals('testing@test.com', $user->email);
在这段代码中,我遇到了一个问题,assertStatus是302,因为您不再重定向它。这只是一个示例,说明我将如何折射您最初编写的内容。尝试删除
assertStatus
呼叫并检查。