F
Filamentβ€’2y ago
Gunnolfson

Cannot delete test

Hi I wonder what's the easiest and most maintenable way of checking something cannot be deleted from a testing point of view. I mean, testing it is not possible from "bulking action", "table action", "page action" etc requires, imo, too much test code. I would rather look for something unique, that would look like this:
test('admin cannot delete', function () {
$admin = AdminUserFactory::new()->create();

$thing = ThingFactory::new()->create();

actingAs($admin, 'admin')
->delete(ThingResource::getUrl('delete', ['record' => $thing->getRouteKey()]))
->assertForbidden();
});
test('admin cannot delete', function () {
$admin = AdminUserFactory::new()->create();

$thing = ThingFactory::new()->create();

actingAs($admin, 'admin')
->delete(ThingResource::getUrl('delete', ['record' => $thing->getRouteKey()]))
->assertForbidden();
});
I've been adviced to use a Laravel policy to implement it. Fair enough but it is not what's relevant here. For some reasons we might not want to use a policy, and the test should be as abstract as possible from the implementations. What I mean is I don't want to have a test on a policy, but rather a "Filament test" that is more close to the feature spec than the code itself Thanks πŸ™ if you have any information about this. Moved to a help post as it's not as simple as I though initially πŸ˜…
4 Replies
Dennis Koch
Dennis Kochβ€’2y ago
If you don't want to repeat those tests invidividually, you can create a method that does it for you
Gunnolfson
GunnolfsonOPβ€’2y ago
So I have to test all these tests, but hide the repetition in a method (in the end there is still one test, but it tests all of the delete then)?
toeknee
toekneeβ€’2y ago
What Dennis is saying is to create a single method or trait which you can then use in your tests that takes a single option, like resource and any additional params
Gunnolfson
GunnolfsonOPβ€’2y ago
Yes, and the method (or trait) checks that for resource given, let's say FooResource, checks it again "bulk action", "page action", and "table action" πŸ€” I guess the signature could be something like
assertItCannotDelete(
resource: $resource,
actor: $actor,
tableAction: $tableAction = true,
bulkAction: $bulkAction = true,
editPageAction: $editPageAction = true
)
assertItCannotDelete(
resource: $resource,
actor: $actor,
tableAction: $tableAction = true,
bulkAction: $bulkAction = true,
editPageAction: $editPageAction = true
)
So that It can be used like so:
test('this admin cannot delete these resource', function () {
$admin = Admin::factory()->create();
$resource = Resource::factory()->create();

assertItCannotDelete($resource, $actor);
});
test('this admin cannot delete these resource', function () {
$admin = Admin::factory()->create();
$resource = Resource::factory()->create();

assertItCannotDelete($resource, $actor);
});
Want results from more Discord servers?
Add your server