Jonjie's avatar
Level 12

Target class [url] does not exist when running test using pest

I'm trying to integrate pest for my laravel 9 project, however, it gives me an error when I run the test. I just followed this https://pestphp.com/docs/plugins#content-laravel

Error

Target class [url] does not exist.

tests/Feature/Controllers/SkillController/IndexTest.php

<?php

use function Pest\Laravel\get;

it('returns a correct json response', function() {
    $url = route('skills.index');
    dd($url);
});
0 likes
2 replies
LaryAI's avatar
Level 58

The error "Target class [url] does not exist" typically indicates that Laravel's service container is unable to resolve a dependency, often due to a missing or misconfigured binding. In this case, it seems related to the route function.

Here are a few steps to troubleshoot and resolve this issue:

  1. Check Route Definition: Ensure that the route skills.index is correctly defined in your routes/web.php or routes/api.php file.

    // routes/web.php or routes/api.php
    Route::get('/skills', [SkillController::class, 'index'])->name('skills.index');
    
  2. Ensure RouteServiceProvider is Loaded: Make sure that the RouteServiceProvider is correctly configured and loaded in your app/Providers/RouteServiceProvider.php.

  3. Clear Cache: Sometimes, cached configurations or routes can cause issues. Clear the cache using the following commands:

    php artisan config:clear
    php artisan route:clear
    php artisan cache:clear
    
  4. Check Namespace: Ensure that your controller is correctly namespaced and autoloaded. For example, if your SkillController is in the App\Http\Controllers namespace, it should be referenced correctly in your route definition.

  5. Update Test Code: Ensure that your test code is correctly using the route function and that the route name is correct.

Here is an example of how your test code should look:

<?php

use function Pest\Laravel\get;

it('returns a correct json response', function() {
    $url = route('skills.index');
    dd($url);
});

If the route is correctly defined and the cache is cleared, this should output the correct URL when you run the test.

  1. Check for Typos: Ensure there are no typos in your route name or controller method.

By following these steps, you should be able to resolve the "Target class [url] does not exist" error and successfully run your Pest test. If the issue persists, double-check your route definitions and ensure that all necessary service providers are correctly configured and loaded.

Jonjie's avatar
Level 12

Okay so I solved it just by running php artisan pest:install, which is currently not indicated in the documentation, or maybe I just overlooked it.

Please or to participate in this conversation.