Search code examples
phpunit-testinglaravel-5httpresponse

Illuminate\Http\Response - Attribute "original" contains string instead of View object


I am returning a View from a controller's method as follows:

return view('Main::pages.content-page', compact('content'));

View renders correctly however, when I dd the response object I see that the attribute "original" of the response object is a string whereas it should have been the View Object.

Response {#385 ▼
    +original: """
    <!DOCTYPE html>\n
    <html lang="en">\n
        <head>\n
        <meta charset="utf-8">\n
        <meta http-equiv="X-UA-Compatible" content="IE=edge">\n
        <meta name="viewport" content="width=device-width, initial-scale=1">\n
        <meta name="author" content="">\n
        <meta name="csrf-token" content="2CQirHnZ7isCBRfkhOYkzPAWOzNIqJISrbb1mfDv">\n
        <meta name="description" content="">\n
        <meta name="keywords" content="">\n
          <title id="page_title">    D&eacute;couvrir\n </title>\n
        .........

Is this expected behavior because my tests are actually failing because of it, since in my test am essentially doing

$this->get(route('main.page', ['content' => $content->slug]))
     ->assertResponseOk()
     ->assertViewHas('content', $content);

Am getting the following failure

F                                                                   1 / 1 (100%)

Time: 179 ms, Memory: 20.00MB

There was 1 failure:

1) IndexControllerTest::testGetPageMethod
The response was not a view.
Failed asserting that false is true.

Looking at the structure of the assertViewHas() method we can see why

/**
 * Assert that the response view has a given piece of bound data.
 *
 * @param  string|array  $key
 * @param  mixed  $value
 * @return $this
 */
public function assertViewHas($key, $value = null)
{
    if (is_array($key)) {
        return $this->assertViewHasAll($key);
    }

    if (! isset($this->response->original) || ! $this->response->original instanceof View) {
        return PHPUnit::assertTrue(false, 'The response was not a view.');
    }
    ......

The condition ! $this->response->original instanceof View fails because original is a string but should have been a View Object.

So am at a lost here. Is this expected behavior if so why the condition in the assertViewHas method ?

I am on Laravel Homestead version '3.0.2'
Laravel 5.2.45

Solution

  • I ran into this same issue. In my case it turned out that the response was receiving a View object when it was prepared, but one of the "after" middleware classes that touched the response before it was returned was converting the View object into a string.

    Are you using the genealabs/laravel-caffeine package by chance? It was that middleware that caused my problem. By setting up that package to not be registered in the testing environment I was able to solve the problem.