【问题标题】:Laravel php testing, called undefined function?Laravel php 测试,调用未定义函数?
【发布时间】:2021-04-22 09:28:22
【问题描述】:

我使用 laravel 8 编写代码,我想为所有模型创建 CRUD 测试,以便我可以在每个测试用例中调用它,例如我有扩展 TestCase 的操作员测试(crud 测试大师)参考:crud test,这是我的操作员测试的样子,..

<?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);
    }
}

这是我的 TestCase.php 的样子,...

<?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;
    }
}

之后,当我 tun php artisan test 时,我收到了这样的错误:

我的代码中有什么磨损吗?我用的是 laravel 8。

【问题讨论】:

    标签: laravel phpunit


    【解决方案1】:

    需要先初始化模型,然后调用模型工厂。

    create 函数在第 64 行未定义。

    代替

    $model = create($model);
    

    使用下面的代码

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

    有关app()->make()factory 的更多信息。

    【讨论】:

    • 在那之后,我得到了这个错误BadMethodCallExceptionSQLite doesn't support dropping foreign keys (you would need to re-create the table).
    • 那个不同的问题,参考github.com/laravel/framework/issues/23461,你问的问题与create函数有关,是未定义的。
    猜你喜欢
    • 2018-02-02
    • 1970-01-01
    • 2016-10-03
    • 2016-03-30
    • 2016-04-24
    • 1970-01-01
    • 2015-09-21
    • 2017-07-20
    • 2015-11-18
    相关资源
    最近更新 更多