Testing - get created record
Hi, when testing the Create Page form, I also associate a relationship using a
Select
.
I can test whether the newly created record is present in the database using assertDatabaseHas
, but how would I check whether also the many-to-many
relationship was stored?
Can I somehow store the resulting new record in a variable (when calling the create
action on the CreatePage test), so that I can then retrieve the ID and check the many-to-many
table in a separate assertDatabaseHas
call?1 Reply
I don´t know how to retrieve the created record id, so I simply assume which record was created and load the model for further testing:
Livewire::actingAs($user)
->test(ArticleCreate::class)
->fill($data)
->call('create')
->assertHasNoFormErrors()
->assertStatus(200)
->assertSessionHas('success', 'Article created successfully.')
->assertRedirect('/article/1');
$this->assertDatabaseCount('articles', 1);
$article = Article::find(1);