samedi 28 avril 2018

Laravel: Is it possible to directly test the json response output of a function without actually going through the URI?

From the docs, I can test some json returned from my app using the following:

$response = $this->json('POST', '/user', ['name' => 'Sally']);

$response
    ->assertStatus(201)
    ->assertJson([
        'created' => true,
    ]);

However, is it possible to bypass actually calling up the URI with $this->json(*method*, *uri*, *data*); and instead test the direct output of a controller function which returns json? For example, I want to do something like this:

// My controller:

function getPageData(){
  $data = array('array', 'of', 'data');
  return response()->json($data);
}

// My Test Class:

$controller = new Primary();
$response = $controller->getPageData();

$response->assertJson([
    'array', 'of', 'data'
]);

Is this possible?

Aucun commentaire:

Enregistrer un commentaire