Testing CRUD made in Laravel with phpunit

Asked

Viewed 323 times

2

Expensive,

I have a simple CRUD, appearing only the description field on the screen, and the code is generated by the database.

I want to put together a script to test: - inclusion - see if it appears in index - amendment - exclusion

But I can’t even open the address because it’s wrong. I developed in Laravel and have to access after login.

Look at the code I developed to test:

$user = User::find(14);
$response = $this->actingAs($user)->get('/login');
$response->assertRedirect('/home');

$response = $this->get('/dfutebol/atividades');
$response->assertStatus(200);

// cria o novo objeto
$obj = new Atividades(['atividade_descricao' => 'Atividade Teste']);
$response = $this->get('/dfutebol/atividades/create', $obj);
$response->assertStatus(200);

And the following error returns to me:

TypeError: Argument 2 passed to Illuminate\Foundation\Testing\TestCase::get() must be of the type array, object given, called in C:\inetpub\wwwroot\sade\tests\Feature\AtividadeTest.php on line 29

C:\inetpub\wwwroot\sade\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:164
C:\inetpub\wwwroot\sade\tests\Feature\AtividadeTest.php:29

Line 29 is the line below:

$response = $this->get('/dfutebol/atividades/create', $obj);

I wonder if someone could give me a hint?

  • The error already tells you, it expects an array with the values, not the object.

1 answer

0

As the error says, it expects an array and an object is being passed. Transform this object into an array before sending it

$obj = new Atividades(['atividade_descricao' => 'Atividade Teste']);
$response = $this->get('/dfutebol/atividades/create', $obj->toArray()); //Objeto está sendo transformado em array
$response->assertStatus(200);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.