Answer the question
In order to leave comments, you need to log in
How to get rid of the Invalid JSON was returned from the route error in tests?
I have test code
public function testRegisterValidate()
{
$responseString = $this->json('POST', '/api/register', ['email' => 'Hello']);
$responseString
->assertStatus(200)
->assertJsonStructure([
'error',
'success',
'message' => ['email', 'password'],
'response'
]);
}
{"error":400,"success":false,"message":"Неверный формат email.","response":[]}
public function register(Request $request)
{
$validate = $this->validator($request->all());
$value = '';
if ($validate){
$value = ResponseApi::response([], 400, false, 400, $validate);
}
return $value;
}
protected function validator(array $data) :string
{
$validator = Validator::make($data, [
'email' => 'required|email|string|max:255|unique:users',
'password' => 'required|string|min:6'
]);
if($validator->fails()){
return $validator->errors()->first();
};
}
public static function response(array $response = [], int $status = 200, bool $success = true, int $error = null, string $message = '', array $headers = [])
{
$headers += ['Content-type' => 'application/json; charset=utf-8'];
$response = [
'error' => $error,
'success' => $success,
'message' => $message,
'response' => $response ? $response : [],
];
return response()->json($response, $status, $headers, JSON_UNESCAPED_UNICODE);
}
Invalid JSON was returned from the route.
/home/ruslan/Разработка/implantBackend/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:695
/home/ruslan/Разработка/implantBackend/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:507
/home/ruslan/Разработка/implant_city_backend/tests/Unit/Auth/ApiAuthControllerTest.php:35
/home/ruslan/Разработка/implant_city_backend/vendor/phpunit/phpunit/src/TextUI/Command.php:206
/home/ruslan/Разработка/implant_city_backend/vendor/phpunit/phpunit/src/TextUI/Command.php:162
->assertHeader('Content-type', 'application/json; charset=utf-8')
Header [Content-type] was found, but value [text/html; charset=UTF-8] does not match [application/json; charset=utf-8].
Failed asserting that two strings are equal.
Expected :'application/json; charset=utf-8'
Actual :'text/html; charset=UTF-8'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question