Muamar Humaidi
Muamar Humaidi

Reputation: 329

Laravel php testing, called undefined function?

I write a code using laravel 8 and i want to create CRUD Testing for all model so i can called it in every test case, for example I Have Operator Test that extends TestCase (crud testing master) ref : crud test, this is my Operator Test looks like,..

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class OperatorTest extends TestCase
{
    use RefreshDatabase, WithFaker;
    public function test_user_can_update_an_operator()
    {
        $this->setBaseRoute('master.operator');
        $this->setBaseModel('App\Models\Operator');
        $this->signIn();
        $this->attributes = [
            'username' => 'test update',
            'level' => 1,
            'category_id' => 1,
            'password' => 'password'
        ];
        $this->update($this->attributes);
    }
}

and this is my TestCase.php looks like,...

<?php

namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use App\Models\Operator;
use Illuminate\Foundation\Testing\WithFaker;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use RefreshDatabase;

    protected $base_route = null;
    protected $base_model = null;


    protected function signIn($user = null)
    {
        $user = $user ?? Operator::factory()->create();
        $this->actingAs($user);
        return $this;
    }

    protected function setBaseRoute($route)
    {
        $this->base_route = $route;
    }

    protected function setBaseModel($model)
    {
        $this->base_model = $model;
    }

    protected function update($attributes = [], $model = '', $route = '')
    {
        $this->withoutExceptionHandling();

        $route = $this->base_route ? "{$this->base_route}.update" : $route;
        $model = $this->base_model ?? $model;

        $model = create($model); 

        if (! auth()->user()) {
            $this->expectException(\Illuminate\Auth\AuthenticationException::class);
        }

        $response = $this->patchJson(route($route, $model->id), $attributes);

        tap($model->fresh(), function ($model) use ($attributes) {
            collect($attributes)->each(function($value, $key) use ($model) {
                $this->assertEquals($value, $model[$key]);
            });
        });

        return $response;
    }
}

after that when I tun php artisan test, i got an error like this : error testting

anything worng in my codes ? i used laravel 8.

Upvotes: 0

Views: 298

Answers (1)

parth
parth

Reputation: 1868

You need to initialize the model first and then call the model factory.

The create function is undefined at line 64.

Instead of

$model = create($model);

Use bellow code

$model = app()->make($model)
$model = $model::factory()->create();

More information on app()->make() and factory.

Upvotes: 2

Related Questions