F
Filament9mo ago
Jo

assertRedirect to a URL with an ID when testing

Here's my Pest code which I write by looking at both the FilamentPHP and Livewire testing docs:
it('redirects to view when post created', function () {
livewire(CreatePost::class)
->fillForm([
'title' => 'Test title'
])
->call('create')
->assertRedirect(ViewPost::class);
});
it('redirects to view when post created', function () {
livewire(CreatePost::class)
->fillForm([
'title' => 'Test title'
])
->call('create')
->assertRedirect(ViewPost::class);
});
This fails saying, Missing required parameter for [Route: filament.admin.resources.posts.view] [URI: admin/posts/{record}] [Missing parameter: record] There doesn't seem to be a way to pass a param to this, so I changed it slightly:
->assertRedirect(route(
'filament.admin.resources.posts.view',
['record' => 16]
));
->assertRedirect(route(
'filament.admin.resources.posts.view',
['record' => 16]
));
Ok, that works. But obviously I'm hardcoding the ID. How am I supposed to get the ID when filling a form like this so that I can assert that it redirected to the correct route dynamically?
Solution:
Query for the latest post. Then you’ll have the id from the create method. For the redirect you should use PostResource::getUrl(‘view’, [‘record’ => $post])...
Jump to solution
2 Replies
Solution
awcodes
awcodes9mo ago
Query for the latest post. Then you’ll have the id from the create method. For the redirect you should use PostResource::getUrl(‘view’, [‘record’ => $post])
Jo
JoOP9mo ago
Nice, thank you! Answers both of my questions 🙂 Here's what I ended up with... I'm posting it here in case it helps anyone else or in case I forget and search again in a few weeks 🤪
->assertRedirect(
PostResource::getUrl('view', ['record' => Post::latest()->first()->id])
)
->assertRedirect(
PostResource::getUrl('view', ['record' => Post::latest()->first()->id])
)
Want results from more Discord servers?
Add your server