Does postJson('/login') instead of post('/login') work?
Nov 28, 2021
9
Level 4
Testing Inertia with Validation
I wonder if anyone can help me out here.
I have a login form built using Inertia and Vue (I know I could use Breeze) and I'm trying to test the validation (using Pest). The test keeps insisting that Invalid JSON was returned from the route. and I'm not sure why. It certainly returns JSON in the browser with a Content-Type: application/json header, after redirecting. The status assertion is passing so I know the test is following redirects as expected.
public function create(): RedirectResponse
{
$validator = validator([
'email' => 'required',
'password' => 'required',
]);
$validator->validate();
return redirect('/login');
}
it('requires an email and password')
->followingRedirects()
->post('/login')
->assertStatus(200)
->assertJsonFragment(['email']);
Level 4
If anybody's having the similar problems, here's how managed to iron things out ...
public function create(LoginRequest $request): RedirectResponse
{
return redirect('/login');
}
class LoginRequest extends FormRequest
{
protected $redirect = "/login";
public function rules()
{
return [
'email' => 'required',
'password' => 'required',
];
}
}
it('requires an email and password')
->followingRedirects()
->post('/login')
->assertInertia(fn (Assert $page) => $page
->component('auth/Login')
->has('errors.email')
->has('errors.password'));
4 likes
Please or to participate in this conversation.