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);
}
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 passing2
(I have no idea what2
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...actingAs($user, 'web')
, but you are making a request to/api
, do you have/api
asweb
guard too? You are mixing them