我正在测试laravel并且昨天看到你在assertSessionHas之前没有跟随重定向。所以我想知道是否有可能在assertSessionHas之后拥有它而不重复整个测试代码。我现在有一个临时解决方案:
$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);
但我想知道您是否能够在以后使用以下重定向更改响应,而不是以这种方式使用它。
答案 0 :(得分:0)
使用PHPUnit在Laravel中发送帖子请求时,您无需传递csrf_token()
,它会自动包含在内。
我会像这样重构你的代码:
$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);