0

I'm having trouble running a test on my API that returns all records from the user model. It's simple. The goal is to retrieve all data from this model. It is working fine both in the browser, in any mode, and in Postman. It always returns the data as JSON. Perfect. That's it. I created both the tests and controllers with Livewire. I'm using the latest version of Laravel, Livewire, etc. I'll share with you guys, the code of the controller that works perfectly, as well as the test code, my routes file, etc. First, here's the error message that appears in my test.

I have already changed the order of routes, I have placed the route in the routes/web.php file. I have cleared the config:clear, cache, etc. I have used the named route, I have undone the named route. I have tested it in Postman and in any browser. It works. It is not a Unit test. It is a Feature Test. It should return the status 200, and the test should not fail with the 404 error.

Please Help.

user@server:/var/www/virtual-card$ sudo php artisan test --filter ApiBuzCardTest::the_api_return_all_users

FAIL  Tests\Feature\Livewire\Api\ApiBuzCardTest
⨯ the api return all users                                                                                                                                                                                                                                  0.08s  
─────────────────────────────────────────────
FAILED  Tests\Feature\Livewire\Api\ApiBuzCardTest > the api return all users                                                                                                                                                              NotFoundHttpException  
GET https://server/virtual-card/api/users

at vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:51
47▕         $handler = $this->container->make(ExceptionHandler::class);
48▕
49▕         $handler->report($e);
50▕
➜  51▕         $response = $handler->render($passable, $e);
52▕
53▕         if (is_object($response) && method_exists($response, 'withException')) {
54▕             $response->withException($e);
55▕         }

      +42 vendor frames 

43  tests/Feature/Livewire/Api/ApiBuzCardTest.php:17

Tests:    1 failed (0 assertions)
Duration: 0.12s

My Controller File: app/Http/Livewire/Api/ApiBuzCard.php

<?php

namespace App\Http\Livewire\Api;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Laravel\Jetstream\Features;
use Illuminate\Http\Request;
use Livewire\Component;

class ApiBuzCard extends Component
{
    public function getAllUsers()
    {
      $users = User::orderByDesc('id')->get()->toJson(JSON_PRETTY_PRINT);

      return response($users, 200);
    }

My Route File: routes/api.php

<?php

use App\Http\Livewire\Api\ApiBuzCard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('users', [ApiBuzCard::class, 'getAllUsers'])->name('api.users');

Route::middleware('auth:sanctum')
    ->get('/user', function (Request $request) {
        return $request->user();
    });

Route::get('user/{id}', [ApiBuzCard::class, 'getUser']);

Route::post('users', [ApiBuzCard::class, 'createUser'])
    ->middleware('api');
Route::put('user/{id}', [ApiBuzCard::class, 'updateUser'])
    ->middleware('api');
Route::delete('user/{id}', [ApiBuzCard::class, 'deleteUser'])
    ->middleware('api');

My Test File tests/Feature/ApiBuzCardTest.php

<?php

namespace Tests\Feature\Livewire\Api;

use App\Http\Livewire\Api\ApiBuzCard;
use App\Models\User;
use Livewire\Livewire;
use Tests\TestCase;

class ApiBuzCardTest extends TestCase
{
    /** @test */
    public function the_api_return_all_users()
    {
        $this->withoutExceptionHandling();

        $user = User::factory()->create();

        $response = $this->get('/api/users');
        $response->assertStatus(200);
    }
14
  • 2
    You did not share the most important part... the test... May 29 at 22:11
  • @matiaslauriti Sorry for this mistake. I edited and provided the code above. May 30 at 12:39
  • So, getJson recives a URL, with headers and JSON ENCODE options... You are passing a URL, an array with a message (that is a header, but I am assuming you want to pass that to the controller, it will not work because it is a GET method, not POST), and then you are passing 2 (I have no idea what 2 is as a JSON ENCODE flag), but it will still make no sense, because again, you cannot JSON encode as you are using GET, so you are not sending anything, just appending to the URL... May 30 at 13:29
  • One more thing, I am checking the results of the test and I see no error at all, did you by mistake remove it from there? Can you check again if you have any exception back? And one more question, you are using actingAs($user, 'web'), but you are making a request to /api, do you have /api as web guard too? You are mixing them May 30 at 13:30
  • Thank you @matiaslauriti. I posted one of the many ways I tried to make the test work. What I want is to pass the test with a 200 status code. I will edit it to show how I am currently doing it. I have been trying for a long time to make this test work. That's because I'm receiving this "notfound" exception. I was guessing that I'm unable to pass the test because I have my API route in the "routes/api.php" file, and there's no "routes/web.php" file, which would be the default file for routes. May 30 at 13:46

1 Answer 1

-1

I found the solution on the link below. The User Sergiu17 suggested that the problem was the following configuration in my phpunit.xml file:

<server name="APP_URL" value="http://devlab03.mshome.net/virtual-card"/>

It needs to be changed to <env name="APP_URL" value="https://localhost"/> Although I couldn't find anything in the official documentation, I believe that the Laravel testing environment can only work with this configuration on localhost. Thank you.

https://laracasts.com/discuss/channels/code-review/laravel-feature-tests-always-return-404

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.