Laravel php 测试,调用未定义函数?
Laravel php testing, called undefined function?
我使用 laravel 8 编写代码,我想为所有模型创建 CRUD 测试,这样我就可以在每个测试用例中调用它,例如我有扩展 TestCase(CRUD 测试大师)ref 的操作员测试: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;
}
}
之后当我调整 php artisan test
时,我得到了这样的错误:
我的代码有什么问题吗?我用了 laravel 8.
需要先初始化模型,再调用模型工厂。
创建函数在第 64 行未定义。
而不是
$model = create($model);
使用下面的代码
$model = app()->make($model)
$model = $model::factory()->create();
有关 app()->make() and factory 的更多信息。
我使用 laravel 8 编写代码,我想为所有模型创建 CRUD 测试,这样我就可以在每个测试用例中调用它,例如我有扩展 TestCase(CRUD 测试大师)ref 的操作员测试: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;
}
}
之后当我调整 php artisan test
时,我得到了这样的错误:
我的代码有什么问题吗?我用了 laravel 8.
需要先初始化模型,再调用模型工厂。
创建函数在第 64 行未定义。
而不是
$model = create($model);
使用下面的代码
$model = app()->make($model)
$model = $model::factory()->create();
有关 app()->make() and factory 的更多信息。