zachleigh's avatar

Using vendor class in test

I need to use a package installed through composer in my test suit, but can't figure out how to get Laravel to instantiate it. Here's my test class:

<?php

use Limelight\Limelight;

class AnalyzerTest extends TestCase
{
    public function setUp()
    {
        $limelight = new Limelight();
    }
}

And I get this error:

PHP Fatal error:  Call to a member function make() on null in /vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 63

In my composer.json file, I have the package Limelight listed under both require and require-dev.

How can I instantiate this class in a test class?

0 likes
10 replies
ifpingram's avatar

That error doesn't seem to correspond to the $limelight = new Limelight(); statement.

Do you still get the error if you comment out the instantiation statement?

zachleigh's avatar

No. If I remove that line, I dont get the error.

ifpingram's avatar

Are you using it anywhere else in the test, or have you only just written the SetUp()? I would also set it to an object variable within the SetUp:

$this->limelight = new Limelight();

corbosman's avatar

The snippet as you posted it should not cause an error. Is there anything else to this test class? It looks like maybe the TestCase class you're extending has a problem. Does your app generally work except for this test?

1 like
corbosman's avatar
Level 20

Actually, it's probably because you're naming your method setUp. Call parent::setUp() in your setUp method, or better, dont call it setUp(). Instead, use an annotation to make sure it gets run before your test.

/**
 * @before
 */
1 like
zachleigh's avatar

Thank you corbosman! After reading your comment, I had a look at the parent TestCase and saw that it contained a createApplication() method. I put that before the Limelight instantiation and it works now.

    public function setUp()
    {
        $this->createApplication();

        $limelight = new Limelight();
    }
`` 
zachleigh's avatar

So I found two ways.

    public function setUp()
    {
        parent::setUp();

        $limelight = new Limelight();
    }
    /**
     * @before
     */
    public function before()
    {
        $limelight = new Limelight();
    }

Thanks @corbosman !

corbosman's avatar

Dont do that. You need to run the parent setUp method, and what you're currently doing prevents that from happening. Just do this:

/**
  * @before 
  */
public function setUpLimeLight() 
{
    $limelight = new Limelight();
}
1 like

Please or to participate in this conversation.