unlikenesses's avatar

Error using Faker in test

I'm following along this screencast, with some changes. I have a test which looks like this:

use App\Client;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ClientTest extends TestCase
{
    use DatabaseTransactions;

    protected $client;

    public function setUp()
    {
        $this->client = factory(App\Client::class)->create();
        // $this->client = new Client(['name' => 'test client']);
    }

    /** @test */
    public function a_client_has_a_name()
    {
        $this->assertEquals('test client', $this->client->name);
    }
}

It produces the following error when I run phpunit:

Fatal error: Call to a member function make() on null in C:\xampp\htdocs\larajobs\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php on line 105

I've run composer update. Line 105 of helpers.php is the return statement in this function:

function app($make = null, $parameters = [])
    {
        if (is_null($make)) {
            return Container::getInstance();
        }

        return Container::getInstance()->make($make, $parameters);
    }

Can anyone help with this?

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

You need to call parent::setUp() before the factory() call

If you define your own setUp method within a test class, be sure to call parent::setUp.

Please or to participate in this conversation.