测试时如何在请求中传递“Accept:application / json”:
例如:
$response = $this->post($route, $data, ['Accept: application/json']);
OR
$response = $this->call('post' $route, $data, [], [], ['Accept: application/json']);
我想在FormRequest类中调用$this->wantsJson()
:
/**
* Get the proper failed validation response for the request.
*
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson()) {
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
答案 0 :(得分:2)