Error: Call to undefined function Tests\factory()

Parsa_237

In laravel I have written the following test:

    public function testUserCanCreateAssortment()
    {
        $this->signIn();

        $this->get('/assortments/create')->assertStatus(200);

        $this->followingRedirects()
            ->post('/assortments', $attributes = Assortment::factory()->raw())
            ->assertSee($attributes['title'])
            ->assertSee($attributes['description']);
    }
}

When I run it with the command phpunit --filter testUserCanCreateItem I get the following error:

Error: Call to undefined function Tests\factory()

No idea what is causing it. I have looked at my factories and my testcase.php but I could not find a solution. What am I doing wrong?

My testcase.php:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected function signIn($user = null)
    {
        $user = $user ?: User::factory()->create();

        $this->actingAs($user);

        return $user;
    }
}

Here the lines the error provides:

/var/www/tests/TestCase.php:13

/var/www/tests/Feature/ItemTest.php:29

Burhan Kashour

In Laravel 8, the factory helper is no longer available. Your testcase model class should use HasFactory trait, then you can use your factory like this:

testcase::factory()->count(50)->create();

Please note that you should also update your call to User factory: factory('App\User')->create()->id;

Here is the relevant documentation: https://laravel.com/docs/8.x/database-testing#creating-models

However, if you prefer to use the Laravel 7.x style factories, you can use the package laravel/legacy-factories You may install it with composer:

composer require laravel/legacy-factories

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

PHP Fatal error: Call to undefined function imagecreatefrompng()

PHP Error: Call to undefined function saveimage

Deployer - Fatal error: Call to undefined function server()

Fatal error: Uncaught Error: Call to undefined function - have to use $this

Call to undefined function iconv() fatal error - Suitecrm

PHP Fatal error: Call to undefined function?

Fatal error: Uncaught Error: Call to undefined function bcadd()

Laravel get Error: Call to undefined function table()!

Uncaught Error: Call to undefined function NOW()

Fatal error: Uncaught Error: Call to undefined function get_option()

Fatal Error: Call to undefined function curl()

Fatal error: Call to undefined function bindParam()

phpMyAdmin Fatal error: Call to undefined function __()

PHP Undefined Object Error and Call a Function on a Non-Object Error

I'm getting error Call to undefined function

Fatal error: Call to undefined function

phpMyAdmin error call to undefined function

Uncaught Error: Call to undefined function

Wordpress/PHP call to undefined function error

Fatal error: Uncaught Error: Call to undefined function mysqli_connect()

curl_init() error Uncaught Error: Call to undefined function

Fatal error: Call to undefined function runQuery()

Uncaught Error: Call to undefined function bindValue() in

Fatal error: Uncaught Error: Call to undefined function mysql_close()

PHPUnit - Error: Call to undefined function in Test file

Fatal error: Uncaught Error: Call to undefined function header() - Beginner question

Uncaught Error: Call to undefined function each()

Fatal error: Uncaught Error: Call to undefined function get_header()