mercredi 1 juillet 2020

What's the best approach to test and force test to go into error lines with PHPUNIT and Laravel?

I've tested successfully these lines, but not the last part which could be an error.

public function store(FormRequestCalendar $request)
{
    $sanitized = $request->getSanitized();

        if ((new Calendar)->create($sanitized))
            return redirect()
                ->route('admin.calendars.index')
                ->with('success', 'Agenda criada com sucesso.');

        return redirect()
            ->back()
            ->with('error', 'Ocorreu um erro ao criar a agenda.');
}

What could I do to force the test to get into these lines?

public function store_returns_an_ok_response()
{
    $calendar = factory(Calendar::class)->make();
        $user = factory(User::class)->create();

        $auth = $this->post(route('login.login', ['cpf' => '000.000.000-00', 'senha' => '123456']));

        $data = [
            'nom_titulo_agenda' => $calendar->nom_titulo_agenda,
            'dat_inicio_evento' => $calendar->dat_inicio_evento->format('d/m/Y H:i'),
            'dat_fim_evento' => $calendar->dat_fim_evento->format('d/m/Y H:i'),
            'dsc_local' => $calendar->dsc_local,
            'dsc_url_anexo' => $calendar->dsc_url_anexo,
            'flg_ativo' => $calendar->flg_ativo,
        ];

        $response = $this->actingAs($user)->post(route('admin.calendars.store'), $data);

        $response->assertRedirect(route('admin.calendars.index'));
}

I've searched through lots of contents but could not make the test.

Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire