Home > OS >  Lumen - Using factory() function in tests make error - Undefined Function
Lumen - Using factory() function in tests make error - Undefined Function

Time:12-19

I'm using Lumen 8.3 ,wanted to use factory() function in my tests, it gives me Undefined Function ,there is nothing useful in the Docs of Lumen

Am i missing something here?

class ProductTest extends TestCase
{

    public function test_if_can_send_products_list(){
        $products = factory('App/Products',5)->make();
        $this->json('post','/payouts',$products)
        ->seeJson([
            'created' => true,
        ]);

    }
}

->

Error: Call to undefined function factory()

CodePudding user response:

It's better to use direct class like that:

$products = factory(Products::class, 5)->create();

don't forget to add Products model usage (namespace).

Edit

You should create Factory:

<?php

namespace Database\Factories;

use App\Products;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class ProductFactory extends Factory
{
    protected $model = Products::class;

    public function definition(): array
    {
        return [
            'name' => $this->faker->unique()->userName()
        ];
    }
}

And add HasFactory Trait to your model:

use Illuminate\Database\Eloquent\Factories\HasFactory;
class Products extends Model {
   use HasFactory;
}

you can also use it like this

Products::factory()->count(5)->make();

CodePudding user response:

I just uncommented these lines in app.php file

$app->withFacades();

$app->withEloquent();
  • Related