0%

Laravel 測試時如何查看 RedirectResponse 的結果

在 Laravel 遇到以下的程式碼時

1
2
3
4
5
6
7
class UserController extends Controller {
public function create(Request $request) {
$user = User::create($request->all());

return redirect(uri('users.show', ['id' => $user->id]));
}
}

在測試的時候想要看到轉址的結果,我們能怎麼做呢?

Laravel >= 5.5.19

1
2
3
4
5
6
7
8
9
class UserControllerTest extends TestCase {
public function test_it_should_show_user_after_create_user() {
$this
->followingRedirects() // 加這行即可
->post('user', [
'name' => 'foo'
]);
}
}

Laravel < 5.4.12:

1
2
3
4
5
6
7
8
9
class UserControllerTest extends TestCase {
public function test_it_should_show_user_after_create_user() {
$this
->followRedirects() // 加這行即可
->post('user', [
'name' => 'foo'
]);
}
}