简体   繁体   中英

Functional testing Laravel ViewComposer

I am trying to test controllers in Laravel using the standard set up this works fine. The test below passes.

class ExampleTest extends TestCase {



/**
 * A basic functional test example.
 *
 * @return void
 */
public function testBasicExample()
{

    $crawler = $this->client->request('GET', '/');
    $this->assertTrue($this->client->getResponse()->isOk());
}

However once I introduce a ViewComposer - registered with a ServiceProvider

class MenuComposer {

const NONE = 0;
const READWRITE = 1;
const READONLY = 2;




/**
 * constructor largely for testing purposes
 * @param View $view
 * @param Auth $auth
 */
public function __construct()
{



}

public function compose($view)
{
    $access = $this->getAccessLevel();

    $view->with('menuItems',$this->menuItems($access));

}

The production code works as expected - but the test fails because it ** ErrorException: Trying to get property of non-object (View: /srv/host_share/sds/app/views/hello.blade.php) **

So the $view variable passed into the compose method is null/empty ? Can anyone suggest what I could do to make tests run and continue to use the viewcomposer functionality?

Based on your error, Laravel's telling you this "Trying to get property of non-object" happens inside the following blade template

(View: /srv/host_share/sds/app/views/hello.blade.php)

It probably means you're passing the view a variable (like menuItems ), and then trying to get a property on that variable

$menuItems->some_prop
$another_var->some_prop

I'd comments out every property access in the view, and add each one back until the error reoccurs. Once you've figured out which view variable isn't an object, you can debug why the testing environment doesn't properly populate it with an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM