5

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");

?

miken32
  • 42,008
  • 16
  • 111
  • 154
user3494047
  • 1,643
  • 4
  • 31
  • 61
  • Why not grab the actual controller method and test to make sure it has the right info? – Caleb Anthony Mar 13 '17 at 21:36
  • Even if I do grab the actual controller, it returns a View. How do I access the Content model object from the view? I guess that would be a solution to my question, but I don't know the answer. – user3494047 Mar 13 '17 at 21:43
  • Checking the [source code](https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Testing/TestResponse.php), it appears you can use something like `->assertViewHas($key, $value)`. – Caleb Anthony Mar 13 '17 at 21:59
  • @CalebAnthony Yes I have seen that but maybe I am getting confused. I know I can do ```->assertViewHas('content', $value)``` but what I want to do is ```->assertViewHas('content->category', $value)``` and this returns null (I think because trying to access the View's 'content' attribute returns an Eloquent object and not a Model. I am not exactly sure. Or because the view doesn't have a 'content->category' attribute. But in any case, i haven't been able to get it to do what I want. – user3494047 Mar 13 '17 at 22:24
  • What version of Laravel 5 are you using? – Rwd Mar 13 '17 at 22:43
  • @RossWilson 5.2.45 – user3494047 Mar 13 '17 at 22:52
  • As long as you're not actually wrapping it in single quotes (which would tell PHP it's a string), it must mean that the value actually doesn't exist. – Caleb Anthony Mar 14 '17 at 22:25
  • Well when I do ```->assertViewHas('content')``` it comes back true. When I dd the View object's 'content' key value doesn't contain the category relationship, in fact it is some eloquent object that I am not too familiar with. That's why I'm asking this question- because im not actually getting back the object I want to be getting back, and I want to know how to do it. – user3494047 Mar 15 '17 at 20:47
  • 1
    Possible duplicate of [How to get view data during unit testing in Laravel](https://stackoverflow.com/questions/21139026/how-to-get-view-data-during-unit-testing-in-laravel) – Vito Meuli Jun 06 '17 at 13:02

4 Answers4

5

You can get your content from the response like this:

$content = $response->getOriginalContent()->getData()['content'];

getData() returns the data sent to the view as an array.

miken32
  • 42,008
  • 16
  • 111
  • 154
Vito Meuli
  • 134
  • 1
  • 7
3

Use assertSee():

$response->assertSee("category 1");
Gabriel Caruso
  • 789
  • 1
  • 7
  • 17
3

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).

Connor Leech
  • 18,052
  • 30
  • 105
  • 150
1

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'];
Ali Obeid
  • 141
  • 7