TiendaWeb
TiendaWeb
PHP
Comparte:

Test Guzzle

Hola! tengo un problema con el test:
Es el consumo de una api con autenticación. No tengo problemas en obtener los datos el problema es en el test 
que me da error.

Clase que consume cliente guzzle:
namespace App\Repositories;
use GuzzleHttp\Client;

class Communes
{
    public function get()
    {

        $client = new Client([
            'base_uri' => 'https://api.shipit.cl/v/',
        ]);

        $headers = [
            'Content-Type' => 'application/json' ,
            'X-Shipit-Email' => 'prueba_front@shipit.cl',
            'X-Shipit-Access-Token' => 'MWhEAdkHKYdscen_4cxR',
            'Accept'        => 'application/vnd.shipit.v2',
        ];

        $reponse = $client->request('GET', 'communes',[
            'headers' => $headers
        ]);

        return json_decode($reponse->getBody()->getContents());
    }

}
Controlador:
namespace App\Http\Controllers;
use App\Repositories\Communes;

class CommunesController extends Controller
{
    protected $communes;

    public function __construct(Communes $communes){
        $this->communes = $communes;
    }

    public function  index(){
        $comunas = $this->communes->get();
        return view('welcome',compact('comunas'));
    }
}

test:
/**
 * @test
 */
 public function shipit_communes(){

    $this->withHeaders([
         'Content-Type' => 'application/json' ,
         'X-Shipit-Email' => 'prueba_front@shipit.cl',
         'X-Shipit-Access-Token' => 'MWhEAdkHKYdscen_4cxR',
         'Accept' => 'application/vnd.shipit.v2',
       ])->json('GET', 'https://api.shipit.cl/v/communes')
         ->assertStatus(200);
   }
error:
Expected status code 200 but received 404.
Failed asserting that false is true.
Jorge García
Jorge García (2780 xp)
Hola, al parecer en el test estás utilizando una ruta global 'https://api.shipit.cl/v/communes' no debería se simplemente 'communes'?
1
TiendaWeb
TiendaWeb (1028 xp)
Gracias Jorge! eso era el problema, es simplemente communes!
salu2