Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
Ruta de prueba de Laravel 5.4 protegida por $request->ajax(), ¿cómo hacer una solicitud de prueba ajax?

Estoy tratando de probar una ruta que hace algo diferente en el controlador, ya sea que la solicitud sea ajax o no.

 public function someAction(Request $request) { if($request->ajax()){ // do something for ajax request return response()->json(['message'=>'Request is ajax']); }else{ // do something else for normal requests return response()->json(['message'=>'Not ajax']); } }

Mi prueba:

 public function testAjaxRoute() { $url = '/the-route-to-controller-action'; $response = $this->json('GET', $url); dd($response->dump()); }

Cuando ejecuto la prueba y simplemente descargo la respuesta, obtengo 'No ajax', lo que tiene sentido, supongo porque $this->json() solo espera una respuesta json, no necesariamente haciendo una solicitud ajax. Pero, ¿cómo puedo probar esto correctamente? He estado comentando el...

 // if($request->ajax(){ ...need to test this code // }else{ // ... // }

cada vez que necesito ejecutar la prueba en esa parte del código. Estoy buscando cómo hacer una solicitud ajax en mi caso de prueba, supongo ...

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

En Laravel 5.4, los métodos this->post() y this->get() aceptan encabezados como tercer parámetro. Establezca HTTP_X-Requested-With en XMLHttpRequest

 $this->post($url, $data, array('HTTP_X-Requested-With' => 'XMLHttpRequest'));

Agregué dos métodos a tests/TestCase.php para hacerlo más fácil.

 <?php namespace Tests; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; /** * Make ajax POST request */ protected function ajaxPost($uri, array $data = []) { return $this->post($uri, $data, array('HTTP_X-Requested-With' => 'XMLHttpRequest')); } /** * Make ajax GET request */ protected function ajaxGet($uri, array $data = []) { return $this->get($uri, array('HTTP_X-Requested-With' => 'XMLHttpRequest')); } }

Luego, desde dentro de cualquier prueba, digamos tests/Feature/HomePageTest.php , puedo hacer lo siguiente:

 public function testAjaxRoute() { $url = '/ajax-route'; $response = $this->ajaxGet($url) ->assertSuccessful() ->assertJson([ 'error' => FALSE, 'message' => 'Some data' ]); }
over 4 years ago · Santiago Trujillo Report

0

Pruebe $response = \Request::create($url, 'GET', ["X-Requested-With" => "XMLHttpRequest"])->json();

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!