dynait's avatar

Test assertViewIs failed when a blade contains <body> HTML element

Hello guys,

Today i faced a very weird issue while i was testing my application. As you have already seen from the title the <body> HTML element inside a blade transforms the test response into a string type instead of a View.

Application:

  • Laravel 5.8.33
  • PHPUnit 7.5.15
  • PHP 7.1.31

I have the following blades:

  • layouts.static-top.default.php
  • default.blade.php

The default.blade.phpextends the layout @extends('layouts.static-top.default'). My layout has the main HTML structure with some sections.

I have the following test:

public function testIndex()
    {

        $response =
            $this->actingAsTankerOperator()
            ->get('/pages/tanker-operator/assessments',$this->jsonHeader);

        $response->assertViewIs('pages.assessments.blades.tankerOperator.default');

        $response->assertViewHas('managers');
    }

I have the following route rule:

...  
...  
Route::get('', 'Assessments\TankerOperatorController@index')
                ->name('tankerOperator.assessments-index');
...
...

I have the following controller method:

public function index(Request $httpRequest)
    {
        try {

             /* @var GenericUser $user */
            $user = $httpRequest->user();
            $managersCollection = $user->managers();


                return view('pages.assessments.blades.tankerOperator.default')
                    ->with('managers',$managersCollection->all())
                   
        } catch (\Error $e) {

            throw new InternalErrorException($e->getMessage());
        }
    }

When i run the testIndex the phpunit returns the error The response is not a view. After some investigation, when i remove the <body> HTLM tag the test is running properly with 2 successful assertions. Has anyone encountered the same issue? I am laughing with this issue... i can't find any solution. It seems that the <body> triggers the render() view method and the response ($response->original) returns the view as a string instead as a View object. I have also tested by removing the layout and use directly in the default.blade the HTML sections and body tags but no luck.

Thank you in advance!

0 likes
1 reply
dynait's avatar

For more details:

The difference is in the Illuminate\View\Compilers\BladeCompiler result:

"blade.compiler" => Illuminate\View\Compilers\BladeCompiler {#948
                    #extensions: []
                    #customDirectives: []
                    #conditions: []
                    #path: "/var/www/html/app/Onlinests/App/Resources/views/layouts/static-top/default.blade.php"
                    #compilers: array:4 [
                      0 => "Comments"
                      1 => "Extensions"
                      2 => "Statements"
                      3 => "Echos"
                    ]
                    #rawTags: array:2 [
                      0 => "{!!"
                      1 => "!!}"
                    ]
                    #contentTags: array:2 [
                      0 => "{{"
                      1 => "}}"
                    ]
                    #escapedTags: array:2 [
                      0 => "{{{"
                      1 => "}}}"
                    ]
                    #echoFormat: "e(%s)"
                    #footer: []
                    #rawBlocks: []
                    #files: Illuminate\Filesystem\Filesystem {#177}
                    #cachePath: "/var/www/html/storage/framework/views"
                    #firstCaseInSwitch: true
                    -encodingOptions: 15
                    #lastSection: "javascript-section"
                    #forElseCounter: 0
                  }

When the body is included in the layout the path and last-section properties are set null.

Please or to participate in this conversation.