Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tobyreed's avatar

Check if endpoint exists

Hello,

I am currently writing tests for a back-end API using Laravel. Some of the endpoints are not made yet and will throw: InvalidArgumentException: Route [Route] not defined. Is there a way to prevent it from completely stopping my tests and continue and give the information later about the failed tests? I want to be able to capture that the response is not a defined route, and then make an if statement to determine if the route is defined or not (dependent on response). This information will then be outputted somewhere else.

Thank you

Toby

0 likes
7 replies
uccdev's avatar

If the endpoints don't exist whatsoever yet, then consider writing them on a placeholder endpoint for now. This may limit the tests you can make, but it offers more in the long run.

If there are any existing endpoints that have similarities with the not-yet-made ones, then consider using that endpoint instead for now, constructing your tests based on those similar elements. i.e similar variables, structure, etc.

I want to be able to capture that the response is not a defined route, and then make an if statement to determine if the route is defined or not (dependent on response). This information will then be outputted somewhere else.

Can you please clarify for me what you mean by a defined route? If you mean a route that exists in your route/web.php file, then maybe these links can help you: https://laracasts.com/discuss/channels/laravel/route-name-exists https://laravel.io/forum/08-02-2016-routehas-issue https://laracasts.com/discuss/channels/general-discussion/check-if-route-exists

Route::has seems to be the simplest solution, at a cursory glance

Talinon's avatar
Talinon
Best Answer
Level 51

@tobyreed

If I understand correctly, just modify the stopOnFailure configuration within your phpunit.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    ...
         stopOnFailure="false"
    ...
>
    ...

This will wait until all the tests have ran before exporting all the errors.

Tray2's avatar

One can but it this way. If you are not following this principle.

  1. Write the test (Red)
  2. Make it pass (Green)
  3. Refactor (or write another test)

Then you might not doing it properly.

Usually you should only write one test at the time and make it pass but when doing test inside out then you will have feature tests that are not passing while writing unit tests to make the feature test pass.

You can always run a single test or a single file with tests to prevent it from blowing up.

Jaytee's avatar

I'd follow @tray2 's advice.

You write it out, it'll fail, you make it pass by meeting it's requirements, then you refactor. Repeat this.

All you need to do is define that route, if that's good enough to make the test pass, then you leave it at that, until it fails again, and that route needs to do something.

1 like
shez1983's avatar

sometimes you need to make the endpoints for your android/ios guys.. if so then a mock response should be sent as that will show you and them what is needed.. ie great for planning

tobyreed's avatar

The issue with this is I am not the developer, but just a tester. So I am unable to "fix" at the same speed I am finding the issues/endpoints that do not exist. So I want a way to be able to determine if the endpoint is not made, and then if it isn't it will spit out some information later on, once the test has completed.

So while yes this is the ideal way to do it, I cannot do it. But thank you

tobyreed's avatar

@shez1983 Could you explain a bit more on this? I like the idea of a 'mock' response that "should" be returned. So that the dev team is able to see what needs to be made and what to use etc

Please or to participate in this conversation.