Reputation: 62
I am still very beginner in testing with the laravel framework. Because of this, I performed tests on the endpoints of my api, but for the post request I have problems with the methods assertOk(), assertStatus() and other it returns an error, but when I leave the test without the asserts it returns a warning. Here is my test code
<?php
namespace Tests\Feature;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Request;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
use Tests\TestCase;
class LocationTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function test_get_all_report()
{
Http::fake([
'http://127.0.0.1:8000/*' => Http::response([
[
"latitude"=> "1.56786467",
"longitude"=> "-0.056789685",
"message"=> "test test that's a place of risk"
],
[
"latitude"=> "1.56786467",
"longitude"=> "-0.056789685",
"message"=> "test test that's a place of risk"
]
], 200),
]);
$response = $this->getJson('api/get-all-signal-zone')->assertStatus(200);
}
/**
* A basic feature test example.
*
* @return void
*/
public function test_get_all_report_catch_error()
{
Http::fake([
'http://127.0.0.1:8000/*' => Http::response([
[
"message"=> "Server error"
]
], 500),
]);
$response = $this->getJson('api/get-all-signal-zone')->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR);
}
/**
* A basic feature test example.
*
* @return void
*/
public function test_get_all_report_by_date()
{
Http::fake();
$response = Http::post('http://127.0.0.1:8000/api/get-all-signal-zone-by-date', [
"date"=> "2022-11-10"
]);
}
}
my route file
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LocationController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('/get-all-signal-zone', [LocationController::class, 'all']);
// Route::get('/get-all-signal-zone-by-date/{date}', [LocationController::class, 'getLocationByDate'])->name('reporting_by_date');
Route::post('/get-all-signal-zone-by-date', [LocationController::class, 'getLocationByDate']);
Route::post('/send-signal-zone', [LocationController::class, 'save']);
And here my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\Location\LocationRepository;
/**
* Description of LocationController
*
* @author Amoungui
*/
class LocationController extends Controller
{
public function __construct(LocationRepository $locationRepository) {
$this->locationRepository = $locationRepository;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function all(){
return $this->locationRepository->list();
}
/**
* View a list of the resource based on a date.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function getLocationByDate(Request $request){
return $this->locationRepository->getByDate($request->date);
}
/**
* Save a newly created resource in external api.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function save(Request $request){
$response = $this->locationRepository->store($request->all());
if($response->successful()){
return response()->json([
'status' => true,
'message' => "location submited successfully",
'data' => $request->all()
], 201);
}
return response()->json([
'data' => null,
'message' => 'Failed',
], 500);
}
}
I have this error
WARN Tests\Feature\LocationTest ✓ get all report ✓ get all report catch error ! get all report by date → This test did not perform any assertions C:\wamp64\www\backend-11-user-story-reporting-of-a-place-in-danger\tests\Feature\LocationTest.php:61
Tests: 1 risky, 4 passed Time: 0.64s
Upvotes: 0
Views: 1287
Reputation: 1289
In Laravel, there is a toolset for mocking anything you need to mock as you can see here. I think you have misunderstood what HTTP::fake()
does. This function mocks outgoing HTTP requests in your code and instead returns an answer without executing the request.
if you're mocking your own endpoint (i.e 127.0.0.1:8000/*
) you're practically preventing Laravel from sending a request to its endpoint and process it as you're returning a fake answer without ever triggering your controller. So the tests are practically pointless at this state.
The main point of HTTP::fake()
(warning, opinion ahead) is to mock calls to external endpoints, for example, if your LocationRepository
sends a request to http://not-a-real-location-api.com
, you'd want to mock that and assume its response since you don't want your tests triggering actual APIs.
Consider removing the use of Http::fake()
entirely since in your case it defeats the purpose of your tests, but just to make this answer whole, note that when Http::fake()
is called without parameters it returns a default empty HTTP 200 response.
Upvotes: 1