'How to get Response variables in a Laravel PHPUnit test?
I am testing a controller method and I am accessing a route in a test.
Then I would like to make sure that the correct model was returned in the view and was loaded with all of the correct relationships.
I know that I can do this:
$this->assertViewHas("content");
But can how can I verify that the content model that was returned into the view has the correct, for example, category? i.e. how can I get the content model object and then do something like
$this->assertEquals($content->category->name, "category 1");
?
Solution 1:[1]
Solution 2:[2]
You can use the following to get the array that's passed to the view:
$response->original->getData()
This comes from Illuminate/Http/ResponseTrait (link to docs).
Solution 3:[3]
You can use
$your_desired_data = $response->assertSee('var_tag');
and if it's an array of data you can access its data by:
$first_name = $your_desired_data['first_name'];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gabriel Caruso |
| Solution 2 | Connor Leech |
| Solution 3 | Ali Obeid |
