Published 1 week ago by bensampo
I'm building an API using Laravel's resources. I want to make sure that the output of each one conforms to my expectations, so for each resource class I have a corresponding unit test.
What I'd ideally like to do is have a test which grabs a list of all of my resource classes and makes sure there is a corresponding unit test.
Essentially I want to test that there are tests.
Is there a sensible/standard way to do this?
I solved it once by just looping over my 'classes to be tested' and looked if there where matching test classes which had a suffix 'Test'. And the same for the methods inside it.
And that was one of the first tests that was ran. Not an ideal solution in all situations but for my case it worked well.
This package from Spatie might be of help for the content/data checking?
To ensure there are tests for each resource is a bit of a rabbit-hole I think. You could write a script or artisan command which looped over all of the files in app/Http//Resources/ and complained (or created) a missing matching test in tests/Api/Resources/ or something - but then you need to remember to run it, so you need a test to test that you've run your script to make your tests ;-)
Or I guess you could add an artisan command like 'make:tested-resource' which itself called 'make:resource' and 'make:test' - but then you need to remember to run that rather than 'make:resource', so you're back to just having to remember to do it :-)