成功创建后的Laravel http响应

After making a POST request to create a new resource, it's common to redirect the browser to a URL for viewing the created resource. Laravel handles this by returning redirect($location) from the controller action.

This is all well and good, except that it makes testing a little bit awkward. It would be great to be able to test these responses with something like:

$this->post(...)->assertSuccessful();

But that specifically tests for an HTTP status code between 200 and 299. Instead, it seems you need to use:

$this->post(...)->assertRedirect($location);

There isn't really a problem with that, but it's not ideal, since it's really testing two different things: that the POST request was successful and that the response redirected the user to the created resource.

Is there a better way to handle this? I thought about using a 201 response with a Location header, but browsers don't seem to follow those.

Starting with laravel 5.5 you can set followingRedirects in the tests:

$this->followingRedirects()->post(...)->assertSuccessful();