R
R
Ruslan Absalyamov2018-10-25 10:30:09
Laravel
Ruslan Absalyamov, 2018-10-25 10:30:09

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'
            ]);
    }

In response I get
{"error":400,"success":false,"message":"Неверный формат email.","response":[]}

Why is my error triggered and how can I check the method?
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();
        };
    }

response API
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);
    }

Sounds completely wrong
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

1 answer(s)
G
Gregory, 2018-10-26
@rusline18

In your validator it says:
something tells me that 'Hello'- a slightly invalid e-mail address!
And yes. If you have a validator 'email', then 'string'you can omit it, and limiting the length of the address is so-so practice!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question