0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

After that, i got this error BadMethodCallException SQLite doesn't support dropping foreign keys (you would need to re-create the table).
That the different issue, refer to github.com/laravel/framework/issues/23461, the question you ask is related to the create function is undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.